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

stop the audit reading an unanswerable merge check as unmerged work, and let a new app opt out

[._.]/ Adam Eivy9bf043aa4b10
diff --git a/server/services/agentRepoStateVerification.js b/server/services/agentRepoStateVerification.js
index 0927fc96c2..96dc3af1ef 100644
--- a/server/services/agentRepoStateVerification.js
+++ b/server/services/agentRepoStateVerification.js
@@ -111,6 +111,21 @@ async function branchHasPendingOwner(branchName, app) {
return pending.some(entry => entry?.prBranch === branchName);
}
+/**
+ * Is `branch` already on `target`? Tri-state, unlike `isBranchMergedInto`, which
+ * answers `false` both for "not merged" and for "could not tell".
+ *
+ * @returns {Promise<boolean|null>} null when either ref could not be resolved
+ */
+async function probeBranchMerged(repoPath, branch, target) {
+ const resolvable = async (ref) => execGit(['rev-parse', '--verify', `${ref}^{commit}`], repoPath, { ignoreExitCode: true })
+ .then(r => r.exitCode === 0)
+ .catch(() => false);
+ const [hasBranch, hasTarget] = await Promise.all([resolvable(branch), resolvable(target)]);
+ if (!hasBranch || !hasTarget) return null;
+ return git.isBranchMergedInto(repoPath, branch, target).catch(() => null);
+}
+
/**
* Probe the repository for the facts the expectation is judged against.
*
@@ -164,26 +179,30 @@ async function probeRepoState({ sourceWorkspace, branchName, worktreePath, branc
// same tri-state contract and both return the change request's state in
// `detail`, so no second round trip is needed to read it.
const probePr = async () => {
- if (!prExpected || !branchShouldBeGone) return { prState: null, prUrl: null, cli: null, readable: true };
+ if (!prExpected || !branchShouldBeGone) return { prState: null, prUrl: null, prNumber: null, cli: null, readable: true };
const forge = await withTimeout(
git.resolveForgeForRepo(sourceWorkspace).catch(() => null),
FORGE_RESOLVE_TIMEOUT_MS,
null
);
- if (!forge?.cli) return { prState: null, prUrl: null, cli: null, readable: false };
+ if (!forge?.cli) return { prState: null, prUrl: null, prNumber: null, cli: null, readable: false };
const { cli, env } = forge;
const found = cli === 'glab'
? await (await import('./gitlab.js')).findMergeRequestForBranch(branchName, sourceWorkspace)
.catch(() => ({ status: 'unavailable' }))
: await (await import('./github.js')).findPullRequestForBranch(branchName, { cwd: sourceWorkspace, env: env || null })
.catch(() => ({ status: 'unavailable' }));
- if (found.status === 'unavailable') return { prState: null, prUrl: null, cli, readable: false };
+ if (found.status === 'unavailable') return { prState: null, prUrl: null, prNumber: null, cli, readable: false };
// `none` is a real answer, not a gap — `verifyPrClaim` already owns "the agent
// never opened one", so there is nothing here to report and nothing missing.
- if (found.status !== 'found') return { prState: null, prUrl: null, cli, readable: true };
+ if (found.status !== 'found') return { prState: null, prUrl: null, prNumber: null, cli, readable: true };
return {
prState: found.detail ? String(found.detail).toUpperCase() : null,
prUrl: found.url || null,
+ // The forge's own identifier for the change request — a GitLab `glab mr
+ // merge` line needs the IID, and emitting a literal `<iid>` placeholder
+ // hands the recovery agent a command it cannot run.
+ prNumber: found.number ?? null,
cli,
readable: !!found.detail,
};
@@ -200,14 +219,19 @@ async function probeRepoState({ sourceWorkspace, branchName, worktreePath, branc
probeLocalBranch(),
probeRemoteBranch(),
probeDefaultBranch(),
- probePr().catch(() => ({ prState: null, prUrl: null, cli: null, readable: false })),
+ probePr().catch(() => ({ prState: null, prUrl: null, prNumber: null, cli: null, readable: false })),
]);
- // Only meaningful while the branch still exists locally — `isBranchMergedInto`
- // answers `false` for a missing ref, which would read as "unmerged work" for
- // the successful case.
+ // Only meaningful while the branch still exists locally.
+ //
+ // `isBranchMergedInto` fails CLOSED — it returns `false`, not a throw, when it
+ // cannot resolve either ref (git.js). That polarity is right for its original
+ // caller, which preserves a branch on doubt; here it inverts into a FINDING, so
+ // an unreadable repo would file a recovery task claiming unmerged work. Resolve
+ // both refs first: if either cannot be read, the answer is `null` (unknown), and
+ // only a `false` backed by two resolvable refs is reported.
const branchMerged = localBranchPresent === true && defaultBranch
- ? await git.isBranchMergedInto(sourceWorkspace, branchName, defaultBranch).catch(() => null)
+ ? await probeBranchMerged(sourceWorkspace, branchName, defaultBranch)
: null;
// Which probes that COULD have produced a finding came back unreadable. The
@@ -234,6 +258,7 @@ async function probeRepoState({ sourceWorkspace, branchName, worktreePath, branc
branchMerged,
prState: pr.prState,
prUrl: pr.prUrl,
+ prNumber: pr.prNumber,
forgeCli: pr.cli,
defaultBranch,
unreadable,
@@ -362,8 +387,8 @@ export const REPO_STATE_REMEDIATIONS = Object.freeze({
// Forge-aware: `driveToMerge` emits a `gh pr merge` procedure, which a GitLab
// recovery agent cannot run. Its non-gh caveats still apply on either forge —
// merge from the repo ROOT, and remove the worktree before deleting the branch.
- [REPO_STATE_ISSUES.PR_UNMERGED]: ({ prUrl, forgeCli }) => (forgeCli === 'glab'
- ? `Finish the merge request${prUrl ? ` ${prUrl}` : ''}: fix failing pipeline jobs, resolve threads, then merge it from the repo ROOT (not inside the branch's worktree) with "glab mr merge <iid> --remove-source-branch". Remove the worktree before deleting the local branch — the delete fails while a worktree still has it checked out.`
+ [REPO_STATE_ISSUES.PR_UNMERGED]: ({ prUrl, prNumber, forgeCli }) => (forgeCli === 'glab'
+ ? `Finish the merge request${prUrl ? ` ${prUrl}` : ''}: fix failing pipeline jobs, resolve threads, then merge it from the repo ROOT (not inside the branch's worktree) with "glab mr merge ${prNumber ?? '<iid>'} --yes --remove-source-branch". Remove the worktree before deleting the local branch — the delete fails while a worktree still has it checked out.`
: `Finish the pull request${prUrl ? ` ${prUrl}` : ''}. ${driveToMerge(prUrl || '<num>')}`),
});
@@ -379,7 +404,7 @@ export const REPO_STATE_REMEDIATIONS = Object.freeze({
async function fileRepoStateRecoveryTask({ agentId, task, branchName, sourceWorkspace, worktreePath, issues, observed, appId }) {
const appName = task?.metadata?.appName || appId || 'PortOS';
const base = observed.defaultBranch || 'the default branch';
- const ctx = { branchName, base, prUrl: observed.prUrl, worktreePath, forgeCli: observed.forgeCli };
+ const ctx = { branchName, base, prUrl: observed.prUrl, prNumber: observed.prNumber, worktreePath, forgeCli: observed.forgeCli };
const context = [
'An agent finished successfully but the repository did not end up in the expected state.',
diff --git a/server/services/agentRepoStateVerification.test.js b/server/services/agentRepoStateVerification.test.js
index 12cc037e3c..4b1a406bf9 100644
--- a/server/services/agentRepoStateVerification.test.js
+++ b/server/services/agentRepoStateVerification.test.js
@@ -353,17 +353,37 @@ describe('verifyAgentRepoState — never fires', () => {
// GitLab is probed too — leaving it unasked would silently pass an open MR as
// a clean repo. States come back lowercase from glab.
resolveForgeForRepo.mockResolvedValue({ cli: 'glab', env: null });
- findMergeRequestForBranch.mockResolvedValue({ status: 'found', url: 'https://gitlab.example.com/mr/4', detail: 'opened' });
+ findMergeRequestForBranch.mockResolvedValue({ status: 'found', url: 'https://gitlab.example.com/mr/4', number: 4, detail: 'opened' });
const result = await run();
expect(findPullRequestForBranch).not.toHaveBeenCalled();
expect(result.issues.map(i => i.code)).toEqual([REPO_STATE_ISSUES.PR_UNMERGED]);
// The remediation must speak glab — a `gh pr merge` line is unrunnable there.
- expect(addTask.mock.calls[0][0].context).toContain('glab mr merge');
+ // The real IID, not a `<iid>` placeholder the recovery agent cannot run.
+ expect(addTask.mock.calls[0][0].context).toContain('glab mr merge 4 --yes --remove-source-branch');
expect(addTask.mock.calls[0][0].context).not.toContain('gh pr merge');
});
+ it('does not claim unmerged work when the merge check could not be answered', async () => {
+ // `isBranchMergedInto` fails CLOSED (`false`, not a throw) when it cannot read
+ // a ref — right for its original caller, inverted into a false finding here.
+ // An unresolvable ref must read as unknown, not as "carries unmerged commits".
+ execGit.mockImplementation((args) => {
+ if (args[0] === 'show-ref') return Promise.resolve({ exitCode: 0, stdout: '', stderr: '' });
+ // rev-parse --verify fails => the ref could not be resolved
+ return Promise.resolve({ exitCode: 128, stdout: '', stderr: 'bad revision' });
+ });
+ isBranchMergedInto.mockResolvedValue(false);
+
+ const result = await run({ prExpected: false });
+
+ expect(isBranchMergedInto).not.toHaveBeenCalled();
+ expect(result.observed.branchMerged).toBeNull();
+ expect(result.issues.map(i => i.code)).not.toContain(REPO_STATE_ISSUES.BRANCH_UNMERGED);
+ expect(result.observed.unreadable).toContain('branch-merged');
+ });
+
it('does not report a MERGED GitLab merge request', async () => {
resolveForgeForRepo.mockResolvedValue({ cli: 'glab', env: null });
findMergeRequestForBranch.mockResolvedValue({ status: 'found', url: 'https://gitlab.example.com/mr/4', detail: 'merged' });
diff --git a/server/services/apps.js b/server/services/apps.js
index 3e67fe93b0..b4859b8097 100644
--- a/server/services/apps.js
+++ b/server/services/apps.js
@@ -413,6 +413,13 @@ export async function createApp(appData) {
// concrete tracker (PLAN.md / GitHub / GitLab / JIRA) from the git origin
// host at dispatch time — see server/lib/workTracker.js.
workTracker: appData.workTracker || 'auto',
+ // Only persisted when explicitly sent. Absent means ON (see
+ // repoStateVerificationEnabled), so writing a default here would freeze the
+ // app against a future change of that default — but an explicit `false` on
+ // create MUST survive, or a new app cannot opt out through POST at all.
+ ...(typeof appData.verifyRepoStateOnCompletion === 'boolean'
+ ? { verifyRepoStateOnCompletion: appData.verifyRepoStateOnCompletion }
+ : {}),
taskTypeOverrides: Object.fromEntries(
SELF_IMPROVEMENT_TASK_TYPES.map(t => [t, { enabled: false }])
),
diff --git a/server/services/apps.test.js b/server/services/apps.test.js
index 151571ceed..9879ce7027 100644
--- a/server/services/apps.test.js
+++ b/server/services/apps.test.js
@@ -230,6 +230,27 @@ describe('portless / desktop apps (#2991)', () => {
expect(stored.devUiPort).toBeNull();
});
+ it('createApp persists an explicit repo-state-audit opt-out, and leaves it absent otherwise', async () => {
+ // Absent means ON (repoStateVerificationEnabled), so createApp must NOT stamp a
+ // default — that would freeze the app against a later change of that default.
+ // But an explicit `false` has to survive, or a new app cannot opt out via POST
+ // at all: createApp builds the record field-by-field and drops anything it
+ // doesn't name.
+ readJSONFile.mockResolvedValue({ apps: { [PORTOS_APP_ID]: { name: 'PortOS' } } });
+
+ const optedOut = await createApp({
+ name: 'Quiet App',
+ repoPath: '/tmp/quiet-app',
+ verifyRepoStateOnCompletion: false,
+ });
+ expect(optedOut.verifyRepoStateOnCompletion).toBe(false);
+ expect(atomicWrite.mock.calls.at(-1)[1].apps[optedOut.id].verifyRepoStateOnCompletion).toBe(false);
+
+ const unset = await createApp({ name: 'Default App', repoPath: '/tmp/default-app' });
+ expect(unset).not.toHaveProperty('verifyRepoStateOnCompletion');
+ expect(atomicWrite.mock.calls.at(-1)[1].apps[unset.id]).not.toHaveProperty('verifyRepoStateOnCompletion');
+ });
+
it('createApp preserves web ports when a separate native target is present', async () => {
readJSONFile.mockResolvedValue({ apps: { [PORTOS_APP_ID]: { name: 'PortOS' } } });
const nativeLaunch = {