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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: preserve active quality snapshot review work (#11324)
[._.]/ Adam Eivy6d173d4be8ee
diff --git a/server/services/appQualitySnapshotAdmission.test.js b/server/services/appQualitySnapshotAdmission.test.jsindex 9f70033659..aa5a842625 100644--- a/server/services/appQualitySnapshotAdmission.test.js+++ b/server/services/appQualitySnapshotAdmission.test.js@@ -57,6 +57,7 @@ const makeDeps = ({ glab = false, onBranch = false, mergeFails = false, ...overraddWorktree: vi.fn(async () => true),execGlab: vi.fn(async () => ''),queuePendingMerge: vi.fn(async () => true),+ checkSnapshotPublicationOwnership: vi.fn(async () => ({ admitted: true })),probePrForBranch: vi.fn(async () => (onBranch? { prState: 'OPEN', prUrl: glab ? glabUrl : prUrl, prNumber: glab ? 7 : 42, cli: glab ? 'glab' : 'gh', readable: true }: { prState: null, readable: true })),diff --git a/server/services/appQualitySnapshotFile.js b/server/services/appQualitySnapshotFile.jsindex 2d95daea30..984b280bf9 100644--- a/server/services/appQualitySnapshotFile.js+++ b/server/services/appQualitySnapshotFile.js@@ -21,7 +21,9 @@* like every other final merge: while a claim holds the lease the PR is left* open and handed to the local pending-merge queue, which takes admission at* its own later attempt. Reads never write. A v1 file or historical- * `quality-snapshot.json` is explicit legacy input.+ * `quality-snapshot.json` is explicit legacy input. Before publication, live+ * review ownership and non-snapshot branch changes defer replacement; the push+ * lease pins only the exact head this invocation inspected.*/import { basename, join } from 'node:path';import { tmpdir } from 'node:os';@@ -183,7 +185,8 @@ function sameSnapshot(stored, next) {async function repoBase(app, git) {if (!await git.isRepo(app.repoPath)) return { skip: skipped(app, 'not-a-repo') };if (!hasOrigin(await git.getRemote(app.repoPath))) return { skip: skipped(app, 'no-remote') };- await git.fetchOrigin(app.repoPath).catch(() => {});+ // A failed fetch cannot authorize replacing a branch from stale local refs.+ await git.fetchOrigin(app.repoPath, { prune: true });const defaultBranch = await git.getDefaultBranch(app.repoPath);if (!defaultBranch) return { skip: skipped(app, 'no-default-branch') };return { defaultBranch };@@ -283,7 +286,43 @@ function mergeRemoteRows(next, stored, deps, options) {return serializeQualitySnapshot(candidate.repository, [...candidate.records, ...(stored.records || [])]);}+async function publicationGuard(app, git, defaultBranch, deps) {+ const probe = deps.probePrForBranch || (await import('./prProbe.js')).probePrForBranch;+ const pr = await probe(app.repoPath, QUALITY_SNAPSHOT_BRANCH).catch(() => null);+ if (pr?.readable !== true || (pr.prState !== null && !['OPEN', 'MERGED', 'CLOSED'].includes(pr.prState))+ || (pr.prState === 'OPEN' && (!pr.prUrl || !pr.prNumber))) {+ return { reason: 'publish-pr-unreadable' };+ }+ const check = deps.checkSnapshotPublicationOwnership+ || (await import('./cosClaimOwnership.js')).checkSnapshotPublicationOwnership;+ const ownership = await check({ appId: app.id, branch: QUALITY_SNAPSHOT_BRANCH, prUrl: pr.prUrl }).catch(() => null);+ if (ownership?.admitted !== true) return { reason: ownership?.reason || 'ownership-unreadable' };+ const remote = await git.execGit(+ ['rev-parse', '--verify', '--quiet', `refs/remotes/origin/${QUALITY_SNAPSHOT_BRANCH}`],+ app.repoPath, { ignoreExitCode: true },+ ).catch(() => null);+ if (remote?.exitCode === 1) return { remoteSha: '' };+ const remoteSha = remote?.stdout?.trim();+ if (remote?.exitCode !== 0 || !/^[0-9a-f]{40,64}$/i.test(remoteSha || '')) return { reason: 'publish-branch-unreadable' };+ // Three-dot comparison excludes unrelated base movement, but retains every+ // change the remote branch would lose when rebuilt from the default branch.+ const diff = await git.execGit(+ ['diff', '--name-only', '-z', `origin/${defaultBranch}...${remoteSha}`, '--'],+ app.repoPath, { ignoreExitCode: true },+ ).catch(() => null);+ if (diff?.exitCode !== 0 || typeof diff.stdout !== 'string') return { reason: 'publish-branch-unreadable' };+ const allowed = new Set([APP_QUALITY_SNAPSHOT_FILENAME, APP_QUALITY_LEGACY_SNAPSHOT_FILENAME]);+ if (diff.stdout.split('\0').filter(Boolean).some(path => !allowed.has(path))) return { reason: 'non-snapshot-changes' };+ return { remoteSha };+}++function deferredPublication(app, reason) {+ return { ...skipped(app, reason), deferredPublication: true };+}+async function landOrReuse(app, git, defaultBranch, deps, next, count, options = {}) {+ const guard = await publicationGuard(app, git, defaultBranch, deps);+ if (guard.reason) return deferredPublication(app, guard.reason);const onBranch = await readGitSnapshot(git, app.repoPath, `origin/${QUALITY_SNAPSHOT_BRANCH}:${APP_QUALITY_SNAPSHOT_FILENAME}`,);@@ -306,7 +345,7 @@ async function landOrReuse(app, git, defaultBranch, deps, next, count, options =try {result = await landSnapshotPr(app, {body: next, count, defaultBranch, git, deps,- message: options.message, removeLegacy: options.removeLegacy === true,+ message: options.message, removeLegacy: options.removeLegacy === true, remoteSha: guard.remoteSha,});} catch (error) {// Another install can advance the shared publish branch after our ref read.@@ -342,6 +381,7 @@ async function landOrReuse(app, git, defaultBranch, deps, next, count, options =// stay retryable: remembering the body would turn the next unchanged cycle// into a 'no-changes' skip and strand the open PR.function rememberPublished(app, body, result) {+ if (result.published !== true) return;if (result.deferredMerge === true && result.queuedMerge !== true) return;lastPublishedBody.set(app.repoPath, body);}@@ -379,7 +419,7 @@ async function landExistingBranchPr(app, git, defaultBranch, deps) {};}-async function landSnapshotPr(app, { body, count, defaultBranch, git, deps, message, removeLegacy = false }) {+async function landSnapshotPr(app, { body, count, defaultBranch, git, deps, message, removeLegacy = false, remoteSha }) {const makeTemp = deps.mkdtemp || mkdtemp;const worktreePath = await makeTemp(join(tmpdir(), 'portos-quality-'));const addWorktree = deps.addWorktree || (async (args, repo) => {@@ -413,7 +453,12 @@ async function landSnapshotPr(app, { body, count, defaultBranch, git, deps, mess).catch(err => git.unstageFiles(worktreePath, commitPaths).catch(() => {}).then(() => Promise.reject(err)));- await pushSnapshotBranch(git, app.repoPath, worktreePath).catch(error => {+ const guard = await publicationGuard(app, git, defaultBranch, deps);+ if (guard.reason) return deferredPublication(app, guard.reason);+ if (guard.remoteSha !== remoteSha) {+ throw Object.assign(new Error('Quality snapshot branch changed during preparation'), { qualitySnapshotStaleLease: true });+ }+ await pushSnapshotBranch(git, worktreePath, remoteSha).catch(error => {if (/stale info/i.test(error?.message || '')) error.qualitySnapshotStaleLease = true;throw error;});@@ -466,26 +511,13 @@ async function cleanupSnapshotWorktree(app, git, worktreePath, removeTemp) {console.warn(`⚠️ Quality snapshot worktree cleanup incomplete for app ${app.id}: ${reasons}`);}-async function pushSnapshotBranch(git, repoPath, worktreePath) {- const remote = await git.execGit(- ['rev-parse', `origin/${QUALITY_SNAPSHOT_BRANCH}`],- repoPath,- { ignoreExitCode: true },- ).catch(() => null);- const remoteSha = remote?.exitCode === 0 ? String(remote.stdout || '').trim() : '';- if (remoteSha && /^[0-9a-f]{7,40}$/i.test(remoteSha)) {- await git.execGit([- 'push',- `--force-with-lease=refs/heads/${QUALITY_SNAPSHOT_BRANCH}:${remoteSha}`,- 'origin',- `HEAD:refs/heads/${QUALITY_SNAPSHOT_BRANCH}`,- ], worktreePath);- return;- }- await git.execGit(- ['push', '-u', 'origin', `HEAD:refs/heads/${QUALITY_SNAPSHOT_BRANCH}`],- worktreePath,- );+async function pushSnapshotBranch(git, worktreePath, remoteSha) {+ // Pin the exact ref whose contents were inspected, including its absence.+ // Re-reading a newer ref here would authorize discarding uninspected work.+ await git.execGit([+ 'push', `--force-with-lease=refs/heads/${QUALITY_SNAPSHOT_BRANCH}:${remoteSha}`,+ 'origin', `HEAD:refs/heads/${QUALITY_SNAPSHOT_BRANCH}`,+ ], worktreePath);}async function openOrAdoptPr(git, cwd, base, deps) {diff --git a/server/services/appQualitySnapshotFile.test.js b/server/services/appQualitySnapshotFile.test.jsindex 4b3764057f..6ad353cef3 100644--- a/server/services/appQualitySnapshotFile.test.js+++ b/server/services/appQualitySnapshotFile.test.js@@ -31,7 +31,7 @@ const gitDouble = (overrides = {}) => ({fetchOrigin: vi.fn(async () => true),getDefaultBranch: vi.fn(async () => 'main'),execGit: vi.fn(async (args) => {- if (args[0] === 'show' || (args[0] === 'rev-parse' && String(args[1] || '').includes(QUALITY_SNAPSHOT_BRANCH))) {+ if (args[0] === 'show' || args[0] === 'rev-parse') {return missingShow;}return { exitCode: 0, stdout: '', stderr: '' };@@ -52,6 +52,7 @@ const testDeps = (overrides = {}) => ({rm: vi.fn(async () => {}),addWorktree: vi.fn(async () => true),queuePendingMerge: vi.fn(async () => true),+ checkSnapshotPublicationOwnership: vi.fn(async () => ({ admitted: true })),// Admission itself is pinned in appQualitySnapshotAdmission.test.js.getOriginInfo: vi.fn(async () => ({ host: 'github.com', fullName: 'example/app' })),withMergeAdmission: vi.fn(async (_origin, work) => ({ admitted: true, result: await work() })),@@ -107,7 +108,7 @@ it('lands a changed snapshot from a detached worktree so the PR branch stays attexpect(deps.git.commit).toHaveBeenCalledWith(worktreePath,'chore: publish PortOS quality snapshot (1 measurements)', { paths: ['.quality.json'] });expect(deps.git.execGit).toHaveBeenCalledWith(- ['push', '-u', 'origin', 'HEAD:refs/heads/portos/quality-snapshot'], worktreePath);+ ['push', '--force-with-lease=refs/heads/portos/quality-snapshot:', 'origin', 'HEAD:refs/heads/portos/quality-snapshot'], worktreePath);expect(deps.git.createPR).toHaveBeenCalledWith(worktreePath, expect.objectContaining({title: 'chore: publish quality snapshot',base: 'main',@@ -248,7 +249,7 @@ it('force-with-lease updates an existing snapshot branch rather than committinggit: gitDouble({execGit: vi.fn(async (args) => {if (args[0] === 'show') return missingShow;- if (args[0] === 'rev-parse' && args[1] === `origin/${QUALITY_SNAPSHOT_BRANCH}`) {+ if (args[0] === 'rev-parse' && args.at(-1) === `refs/remotes/origin/${QUALITY_SNAPSHOT_BRANCH}`) {return { exitCode: 0, stdout: 'aa'.repeat(20), stderr: '' };}return { exitCode: 0, stdout: '', stderr: '' };@@ -354,7 +355,7 @@ it('does not retry a stale lease when the refreshed PR state cannot be read', as});const deps = testDeps({git,- probePrForBranch: vi.fn(async () => ({ readable: false })),+ probePrForBranch: vi.fn().mockResolvedValueOnce({ readable: true, prState: null }).mockResolvedValueOnce({ readable: true, prState: null }).mockResolvedValue({ readable: false }),});await expect(publishAppQualitySnapshot(app, deps)).rejects.toThrow('publish PR state unavailable');expect(deps.probePrForBranch).toHaveBeenCalledWith(app.repoPath, QUALITY_SNAPSHOT_BRANCH);@@ -691,3 +692,169 @@ it('preserves open-branch history during explicit format migration without applyexpect(written.categories).toEqual(['privacy', 'security']);expect(written.measurements).toHaveLength(2);});++// #11324: exported publication workflow with the real read-only ownership+// boundary. Only persisted state, worktrees and forge/git I/O are synthetic.+const ownedPublicationDeps = ({ agents = {}, trusted = true, worktrees = [], ...overrides } = {}) => {+ const ownership = { agents, trusted, worktrees };+ const deps = testDeps({+ checkSnapshotPublicationOwnership: async input => {+ const { checkSnapshotPublicationOwnership } = await import('./cosClaimOwnership.js');+ return checkSnapshotPublicationOwnership(input, {+ readAgentsStateForSafetyCheck: async () => ({ trusted: ownership.trusted, agents: ownership.agents }),+ getAppById: async () => app,+ listWorktrees: async () => ownership.worktrees,+ getActiveAgentIds: () => [],+ });+ },+ probePrForBranch: vi.fn(async () => ({ readable: true, prState: 'OPEN', prUrl, prNumber: 42, cli: 'gh' })),+ ...overrides,+ });+ return { deps, ownership };+};+const reviewOwner = status => ({+ id: 'reviewer', status,+ metadata: { reviewLoopMergeTarget: { url: prUrl, branch: QUALITY_SNAPSHOT_BRANCH } },+});+const branchGit = (changes = '.quality.json\0') => gitDouble({+ execGit: vi.fn(async args => {+ if (args[0] === 'show') return missingShow;+ if (args[0] === 'rev-parse') return { exitCode: 0, stdout: 'aa'.repeat(20) };+ if (args[0] === 'diff') return { exitCode: 0, stdout: changes };+ return { exitCode: 0, stdout: '' };+ }),+});+const expectNoPublication = deps => {+ expect(deps.git.execGit.mock.calls.filter(([args]) => args[0] === 'push')).toEqual([]);+ expect(deps.git.createPR).not.toHaveBeenCalled();+ expect(deps.git.mergePR).not.toHaveBeenCalled();+ expect(deps.queuePendingMerge).not.toHaveBeenCalled();+};++it('retains deferred measurements through active and paused review, then publishes when the review completes', async () => {+ const { deps, ownership } = ownedPublicationDeps({ agents: { reviewer: reviewOwner('running') }, git: branchGit() });+ const original = structuredClone(ownership.agents);+ expect(await publishAppQualitySnapshot(app, deps)).toMatchObject({+ published: false, deferredPublication: true, reason: 'review-owner-active',+ });+ expect(ownership.agents).toEqual(original);+ ownership.agents.reviewer.status = 'paused';+ expect(await publishAppQualitySnapshot(app, deps)).toMatchObject({ reason: 'review-owner-active' });+ expectNoPublication(deps);+ expect(deps.addWorktree).not.toHaveBeenCalled();++ ownership.agents.reviewer.status = 'completed';+ expect(await publishAppQualitySnapshot(app, deps)).toMatchObject({ published: true, merged: true });+ expect(deps.buildQualitySnapshot).toHaveBeenCalledTimes(3);+ expect(deps.writeFile).toHaveBeenCalledWith(join(worktreePath, '.quality.json'), canonical());+});++it('rechecks ownership before pushing and retries an unchanged body after a late owner releases it', async () => {+ const { deps, ownership } = ownedPublicationDeps({ git: branchGit() });+ deps.git.commit.mockImplementationOnce(async () => {+ ownership.agents.reviewer = reviewOwner('running');+ return { hash: 'abc1234' };+ });+ expect(await publishAppQualitySnapshot(app, deps)).toMatchObject({ reason: 'review-owner-active', deferredPublication: true });+ expectNoPublication(deps);+ expect(deps.rm).toHaveBeenCalledWith(worktreePath, { recursive: true, force: true });+ ownership.agents = {};+ expect(await publishAppQualitySnapshot(app, deps)).toMatchObject({ published: true });+ expect(deps.git.commit).toHaveBeenCalledTimes(2);+});++it('preserves a live review checkout even when no PR owner target was registered', async () => {+ const holder = { path: '/tmp/agent-reviewer', branch: `refs/heads/${QUALITY_SNAPSHOT_BRANCH}` };+ const { deps, ownership } = ownedPublicationDeps({+ worktrees: [holder], agents: { reviewer: { id: 'reviewer', status: 'running', metadata: { workspacePath: holder.path } } },+ });+ expect(await publishAppQualitySnapshot(app, deps)).toMatchObject({ published: false, deferredPublication: true });+ expectNoPublication(deps);+ expect(ownership.worktrees).toEqual([holder]);+ expect(deps.rm).not.toHaveBeenCalled();+});++it.each([+ { trusted: false, agents: {} },+ { trusted: true, agents: null },+ { trusted: true, agents: { broken: 'unreadable' } },+])('fails closed on unreadable ownership %#', async input => {+ const { deps } = ownedPublicationDeps(input);+ expect(await publishAppQualitySnapshot(app, deps)).toMatchObject({ reason: 'ownership-unreadable', deferredPublication: true });+ expectNoPublication(deps);+});++it('defers an unreadable forge or rejected ownership lookup without marking the body delivered', async () => {+ const deps = testDeps({ probePrForBranch: vi.fn().mockRejectedValueOnce(new Error('forge offline')).mockResolvedValue({ readable: true, prState: null }) });+ expect(await publishAppQualitySnapshot(app, deps)).toMatchObject({ reason: 'publish-pr-unreadable' });+ deps.checkSnapshotPublicationOwnership.mockRejectedValueOnce(new Error('state read failed'));+ expect(await publishAppQualitySnapshot(app, deps)).toMatchObject({ reason: 'ownership-unreadable' });+ expectNoPublication(deps);+ expect(await publishAppQualitySnapshot(app, deps)).toMatchObject({ published: true });+});++it('preserves foreign review code without a local owner, including when snapshot bytes already match', async () => {+ const git = branchGit('.quality.json\0server/reviewFix.js\0');+ const originalExec = git.execGit.getMockImplementation();+ git.execGit.mockImplementation(args => args[0] === 'show' && args[1].includes(QUALITY_SNAPSHOT_BRANCH)+ ? Promise.resolve({ exitCode: 0, stdout: canonical() }) : originalExec(args));+ const { deps } = ownedPublicationDeps({ git });+ expect(await publishAppQualitySnapshot(app, deps)).toMatchObject({ reason: 'non-snapshot-changes', deferredPublication: true });+ expectNoPublication(deps);+ expect(deps.addWorktree).not.toHaveBeenCalled();+});++it('does not discard foreign review code discovered during a stale-lease retry', async () => {+ let pushes = 0;+ const git = branchGit();+ const originalExec = git.execGit.getMockImplementation();+ git.execGit.mockImplementation(async args => {+ if (args[0] === 'push') { pushes++; throw new Error('! [rejected] (stale info)'); }+ if (args[0] === 'diff' && pushes) return { exitCode: 0, stdout: '.quality.json\0server/reviewFix.js\0' };+ return originalExec(args);+ });+ const { deps } = ownedPublicationDeps({ git });+ expect(await publishAppQualitySnapshot(app, deps)).toMatchObject({ reason: 'non-snapshot-changes', deferredPublication: true });+ expect(pushes).toBe(1);+ expect(deps.addWorktree).toHaveBeenCalledTimes(1);+ expect(git.createPR).not.toHaveBeenCalled();+});++it('defers unreadable branch contents rather than treating a failed inspection as an empty diff', async () => {+ const git = branchGit();+ const originalExec = git.execGit.getMockImplementation();+ git.execGit.mockImplementation(args => args[0] === 'diff'+ ? Promise.resolve({ exitCode: 128, stdout: '' }) : originalExec(args));+ const deps = testDeps({ git });+ expect(await publishAppQualitySnapshot(app, deps)).toMatchObject({ reason: 'publish-branch-unreadable', deferredPublication: true });+ expectNoPublication(deps);+});++it('re-inspects a ref moved by another fetch during preparation instead of leasing its unchecked head', async () => {+ let moved = false;+ const git = branchGit();+ const originalExec = git.execGit.getMockImplementation();+ git.commit.mockImplementation(async () => { moved = true; return { hash: 'abc1234' }; });+ git.execGit.mockImplementation(args => args[0] === 'rev-parse' && moved+ ? Promise.resolve({ exitCode: 0, stdout: 'bb'.repeat(20) }) : originalExec(args));+ const deps = testDeps({ git });+ expect(await publishAppQualitySnapshot(app, deps)).toMatchObject({ published: true });+ expect(deps.addWorktree).toHaveBeenCalledTimes(2);+ const pushes = git.execGit.mock.calls.filter(([args]) => args[0] === 'push');+ expect(pushes).toEqual([[[+ 'push', `--force-with-lease=refs/heads/${QUALITY_SNAPSHOT_BRANCH}:${'bb'.repeat(20)}`,+ 'origin', `HEAD:refs/heads/${QUALITY_SNAPSHOT_BRANCH}`,+ ], worktreePath]]);+});++it('applies the same ownership protection to explicit snapshot migrations', async () => {+ const git = branchGit();+ const originalExec = git.execGit.getMockImplementation();+ git.execGit.mockImplementation(args => args[0] === 'show' && args[1] === 'origin/main:.quality.json'+ ? Promise.resolve({ exitCode: 0, stdout: serialized(snapshot()) }) : originalExec(args));+ const { deps, ownership } = ownedPublicationDeps({ git, agents: { reviewer: reviewOwner('running') } });+ expect(await migrateAppQualitySnapshot(app, deps)).toMatchObject({ reason: 'review-owner-active', deferredPublication: true });+ expectNoPublication(deps);+ ownership.agents = {};+ expect(await migrateAppQualitySnapshot(app, deps)).toMatchObject({ published: true });+});diff --git a/server/services/cosClaimOwnership.js b/server/services/cosClaimOwnership.jsindex 42bc577dac..695e34e307 100644--- a/server/services/cosClaimOwnership.js+++ b/server/services/cosClaimOwnership.js@@ -119,3 +119,27 @@ export async function checkReconcileOwnership(input, deps = {}) {return admission;}));}++/** Read-only snapshot publication gate. Registered review work remains owned+ * even between checkouts; neither an absent process nor a missing tree releases+ * it. The safety reader must not default corrupt runtime state to an empty list.+ */+export async function checkSnapshotPublicationOwnership({ appId, branch, prUrl }, deps = {}) {+ const readAgents = deps.readAgentsStateForSafetyCheck+ || (await import('./cosState.js')).readAgentsStateForSafetyCheck;+ const snapshot = await readAgents().catch(() => null);+ if (snapshot?.trusted !== true || !snapshot.agents || typeof snapshot.agents !== 'object' || Array.isArray(snapshot.agents)) {+ return { admitted: false, reason: 'ownership-unreadable' };+ }+ const entries = Object.entries(snapshot.agents);+ if (entries.some(([id, agent]) => !agent || agent.id !== id || !['running', 'paused', 'completed'].includes(agent.status))) {+ return { admitted: false, reason: 'ownership-unreadable' };+ }+ const agents = entries.map(([, agent]) => agent);+ if (prUrl && agents.some(agent => agent.status !== 'completed'+ && agent.metadata?.reviewLoopMergeTarget?.url === prUrl+ && agent.metadata.reviewLoopMergeTarget.branch === branch)) {+ return { admitted: false, reason: 'review-owner-active' };+ }+ return checkReconcileOwnership({ appId, branch }, { ...deps, getAgents: async () => agents });+}