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: reject wrong-typed reasoner envelope fields as malformed output (#4166)
Adam Eivyf5e6f4899c16
diff --git a/.changelog/next/fixed-issue-4166.md b/.changelog/next/fixed-issue-4166.mdnew file mode 100644index 0000000000..3530afc1de--- /dev/null+++ b/.changelog/next/fixed-issue-4166.md@@ -0,0 +1 @@+- Layered Intelligence now records a wrong-typed reasoner envelope field (e.g. a non-string `analysis`, an unresolvable `pause`) as `unparseable-response` instead of counting it as a successful `no-proposal` rundiff --git a/server/services/autonomousJobs/layeredIntelligenceHooks.js b/server/services/autonomousJobs/layeredIntelligenceHooks.jsindex 461b1fa28a..0ca3ce69a9 100644--- a/server/services/autonomousJobs/layeredIntelligenceHooks.js+++ b/server/services/autonomousJobs/layeredIntelligenceHooks.js@@ -589,20 +589,24 @@ export async function processTaskOutput({ appId, success, payload, agentId } = {// envelope only requires `payload` to be an object, and salvageSentinelPayload's// lenient extractor can surface a non-envelope object out of prose.const envelope = isTaskOutputPayload(payload) ? payload : null- const { proposal, pause } = validateReasonerResponse(envelope)-- // A reasoner that SUPPLIED a non-null proposal which then failed validation- // (missing/unknown scope, no title, unnormalizable slug) did not "look and find- // nothing" — it tried to propose and emitted the wrong shape. Both used to land- // on `no-proposal` → success. `proposal: null` stays the legitimate empty answer.- // Narrow on purpose: validateReasonerResponse is documented to drop invalid- // pieces leniently, and this only reclassifies the field that IS the deliverable.- const proposalAttemptedButInvalid = envelope != null && !proposal && envelope.proposal != null+ const { proposal, invalidFields = [], pause } = validateReasonerResponse(envelope)++ // A reasoner that SUPPLIED an envelope field which then failed validation did not+ // "look and find nothing" — it tried to answer and emitted the wrong shape. That+ // used to land on `no-proposal` → success. Originally (#2727) only the `proposal`+ // field was reclassified; #4166 extends it to every supplied-but-unusable field+ // (`{"analysis": 7}`, a `pause` with no resolvable target), since+ // validateReasonerResponse now reports them all in `invalidFields`. A `null` field+ // is ABSENT, not malformed, so `proposal: null` stays the legitimate empty answer.+ // A null envelope never reaches here with a non-empty list (the validator returns+ // the empty triple for it), and the ternary below short-circuits on it anyway; the+ // `= []` destructuring default keeps a stubbed validator from throwing.+ const malformedEnvelope = invalidFields.length > 0let filedNumber = nulllet filedKey = nulllet filedAction = 'no-op'- let reason = envelope == null || proposalAttemptedButInvalid ? 'unparseable-response' : 'no-proposal'+ let reason = envelope == null || malformedEnvelope ? 'unparseable-response' : 'no-proposal'let handedOff = false// §4 (#2764): when the deterministic routing gate files-for-human instead of// auto-handing-off a trivial+safe proposal, surface WHY on the returned result.diff --git a/server/services/autonomousJobs/layeredIntelligenceHooks.test.js b/server/services/autonomousJobs/layeredIntelligenceHooks.test.jsindex f38628af87..b4ee5d427d 100644--- a/server/services/autonomousJobs/layeredIntelligenceHooks.test.js+++ b/server/services/autonomousJobs/layeredIntelligenceHooks.test.js@@ -51,7 +51,7 @@ vi.mock('../layeredIntelligence.js', () => ({listForgeIssues: vi.fn().mockResolvedValue({ ok: true, issues: [] }),listBlockingIssues: vi.fn().mockResolvedValue({ ok: true, issues: [] }),isAppParked: vi.fn(() => false),- validateReasonerResponse: vi.fn(() => ({ proposal: null, pause: null })),+ validateReasonerResponse: vi.fn(() => ({ proposal: null, pause: null, invalidFields: [] })),isScopeAllowed: vi.fn(() => true),isProposalDuplicate: vi.fn(() => false),checkSemanticDuplicate: vi.fn().mockResolvedValue({ available: false, duplicate: false }),@@ -557,7 +557,7 @@ describe('processTaskOutput', () => {});it('records unparseable-response when the payload is null', async () => {- li.validateReasonerResponse.mockReturnValue({ proposal: null, pause: null });+ li.validateReasonerResponse.mockReturnValue({ proposal: null, pause: null, invalidFields: [] });const out = await processTaskOutput({ appId: 'app-1', success: true, payload: null });expect(out).toMatchObject({ action: 'no-op', reason: 'unparseable-response' });});@@ -569,7 +569,7 @@ describe('processTaskOutput', () => {// indistinguishable from a correct empty answer and got recorded as a// successful run. `{}` is reachable: the sentinel envelope only requires// `payload` to be an object.- li.validateReasonerResponse.mockReturnValue({ proposal: null, pause: null });+ li.validateReasonerResponse.mockReturnValue({ proposal: null, pause: null, invalidFields: [] });for (const payload of ['just some prose', 42, ['a', 'b'], true, {}, { foo: 1 }]) {const out = await processTaskOutput({ appId: 'app-1', success: true, payload });expect(out).toMatchObject({ action: 'no-op', reason: 'unparseable-response' });@@ -580,7 +580,7 @@ describe('processTaskOutput', () => {// The other side of the sentinel: the reasoner answered correctly and simply// had nothing to file. That is a successful run, not malformed output. Any ONE// documented key makes it a real answer.- li.validateReasonerResponse.mockReturnValue({ proposal: null, pause: null });+ li.validateReasonerResponse.mockReturnValue({ proposal: null, pause: null, invalidFields: [] });for (const payload of [{ analysis: 'nothing worth proposing', proposal: null }, { proposal: null }, { analysis: '' }]) {const out = await processTaskOutput({ appId: 'app-1', success: true, payload });expect(out).toMatchObject({ action: 'no-op', reason: 'no-proposal' });@@ -591,13 +591,29 @@ describe('processTaskOutput', () => {// The reasoner ATTEMPTED a proposal and emitted the wrong shape (no scope/title,// bad slug). That is malformed output, not "I looked and found nothing" — both// used to land on `no-proposal` and count as a successful run.- li.validateReasonerResponse.mockReturnValue({ proposal: null, pause: null });+ li.validateReasonerResponse.mockReturnValue({ proposal: null, pause: null, invalidFields: ['proposal'] });const out = await processTaskOutput({ appId: 'app-1', success: true, payload: { analysis: 'x', proposal: { title: '' } } });expect(out).toMatchObject({ action: 'no-op', reason: 'unparseable-response' });});+ it('records unparseable-response for a wrong-typed NON-deliverable field (#4166)', async () => {+ // `{"analysis": 7}` is an envelope (it carries a documented key) that supplies a+ // field it then gets wrong. Pre-#4166 the deliverable-only check let it through as+ // `no-proposal`, i.e. a successful run indistinguishable from a correct empty+ // answer. Any field validateReasonerResponse reports in `invalidFields` now counts.+ for (const [payload, invalidFields] of [+ [{ analysis: 7 }, ['analysis']],+ [{ analysis: 'x', pause: { reason: 'no target' } }, ['pause']],+ [{ analysis: [], pause: 3 }, ['analysis', 'pause']]+ ]) {+ li.validateReasonerResponse.mockReturnValue({ proposal: null, pause: null, invalidFields });+ const out = await processTaskOutput({ appId: 'app-1', success: true, payload });+ expect(out).toMatchObject({ action: 'no-op', reason: 'unparseable-response' });+ }+ });+it('treats an explicit proposal:null as a legitimate empty answer, not malformed (#2727)', async () => {- li.validateReasonerResponse.mockReturnValue({ proposal: null, pause: null });+ li.validateReasonerResponse.mockReturnValue({ proposal: null, pause: null, invalidFields: [] });const out = await processTaskOutput({ appId: 'app-1', success: true, payload: { analysis: 'nothing to propose', proposal: null } });expect(out).toMatchObject({ action: 'no-op', reason: 'no-proposal' });});@@ -606,7 +622,7 @@ describe('processTaskOutput', () => {// readIssues is an unbounded forge call and only the has-a-proposal branch// consumes it. Since the #2727 hoist the hook runs while the agent still holds// a CoS concurrency slot, so the common no-op path must not shell out to `gh`.- li.validateReasonerResponse.mockReturnValue({ proposal: null, pause: null });+ li.validateReasonerResponse.mockReturnValue({ proposal: null, pause: null, invalidFields: [] });li.listForgeIssues.mockClear();await processTaskOutput({ appId: 'app-1', success: true, payload: { analysis: 'nothing to do', proposal: null } });expect(li.listForgeIssues).not.toHaveBeenCalled();diff --git a/server/services/layeredIntelligence.test.js b/server/services/layeredIntelligence.test.jsindex c406cb08e5..beadbad0ee 100644--- a/server/services/layeredIntelligence.test.js+++ b/server/services/layeredIntelligence.test.js@@ -375,9 +375,10 @@ describe('validateReasonerResponse', () => {});it('handles garbage input without throwing', () => {- expect(validateReasonerResponse(null)).toEqual({ analysis: '', proposal: null, pause: null });- expect(validateReasonerResponse('nope')).toEqual({ analysis: '', proposal: null, pause: null });- expect(validateReasonerResponse({ proposal: [], pause: [] })).toEqual({ analysis: '', proposal: null, pause: null });+ expect(validateReasonerResponse(null)).toEqual({ analysis: '', proposal: null, pause: null, invalidFields: [] });+ expect(validateReasonerResponse('nope')).toEqual({ analysis: '', proposal: null, pause: null, invalidFields: [] });+ expect(validateReasonerResponse({ proposal: [], pause: [] }))+ .toEqual({ analysis: '', proposal: null, pause: null, invalidFields: ['proposal', 'pause'] });});it('normalizes proposal complexity + safe (defaults: null / false)', () => {@@ -394,6 +395,47 @@ describe('validateReasonerResponse', () => {expect(junk.proposal.complexity).toBe(null);expect(junk.proposal.safe).toBe(false);});++ describe('invalidFields (#4166)', () => {+ it('reports a wrong-typed analysis and drops it', () => {+ const r = validateReasonerResponse({ analysis: 7 });+ expect(r.analysis).toBe('');+ expect(r.invalidFields).toEqual(['analysis']);+ });++ it('reports a supplied-but-unusable proposal', () => {+ expect(validateReasonerResponse({ proposal: { scope: 'nuke', slug: 'x', title: 'X' } }).invalidFields).toEqual(['proposal']);+ expect(validateReasonerResponse({ proposal: 'do the thing' }).invalidFields).toEqual(['proposal']);+ });++ it('reports a supplied-but-unusable pause', () => {+ expect(validateReasonerResponse({ pause: { blockOnIssue: 42, reason: '' } }).invalidFields).toEqual(['pause']);+ expect(validateReasonerResponse({ pause: { reason: 'no target' } }).invalidFields).toEqual(['pause']);+ // "this" with nothing to block on is malformed, not an empty answer.+ expect(validateReasonerResponse({ proposal: null, pause: { blockOnIssue: 'this', reason: 'x' } }).invalidFields).toEqual(['pause']);+ });++ it('treats an absent or explicitly-null field as absent, never malformed', () => {+ expect(validateReasonerResponse({ analysis: 'nothing to propose', proposal: null, pause: null }).invalidFields).toEqual([]);+ expect(validateReasonerResponse({ proposal: null }).invalidFields).toEqual([]);+ expect(validateReasonerResponse({ analysis: '' }).invalidFields).toEqual([]);+ expect(validateReasonerResponse({}).invalidFields).toEqual([]);+ });++ it('stays empty for a fully valid envelope', () => {+ const r = validateReasonerResponse({+ analysis: 'a',+ proposal: { scope: 'app-improvement', slug: 'x', title: 'X' },+ pause: { blockOnIssue: 'this', reason: 'block on the new one' }+ });+ expect(r.invalidFields).toEqual([]);+ });++ it('collects every unusable field, in envelope order', () => {+ const r = validateReasonerResponse({ analysis: [], proposal: {}, pause: 3 });+ expect(r.invalidFields).toEqual(['analysis', 'proposal', 'pause']);+ });+ });});describe('isHandoffEligible', () => {diff --git a/server/services/layeredIntelligence/proposal.js b/server/services/layeredIntelligence/proposal.jsindex be6f7fdbc2..35f60e02b8 100644--- a/server/services/layeredIntelligence/proposal.js+++ b/server/services/layeredIntelligence/proposal.js@@ -12,17 +12,29 @@ import { normalizeSlug } from './dedup.js';/*** Validate + normalize the reasoner's JSON. Returns- * `{ analysis, proposal, pause }` with invalid pieces dropped (never throws):+ * `{ analysis, proposal, pause, invalidFields }` with invalid pieces dropped+ * (never throws):+ * - `analysis` kept only when it is a string.* - `proposal` kept only when it has a recognized scope + a normalizable slug* + a non-empty title. `slug` is normalized in place.* - `pause` kept only when it has a reason AND a resolvable target: an integer* issue number, or `"this"` WITH a surviving proposal to block on. A* `pause.blockOnIssue: "this"` with a null proposal is invalid → dropped.+ *+ * `invalidFields` (#4166) names every envelope key that was SUPPLIED (present and+ * non-null) but could not be used — the sentinel that separates "the reasoner+ * emitted the wrong shape" from "the reasoner had nothing to say". Dropping is+ * still lenient (callers that only want the usable pieces can ignore it), but a+ * caller deciding whether a run was malformed must not have to re-derive per-field+ * validity: `{"analysis": 7}` reads as a legitimate empty answer without this.+ * `null`/`undefined` is ABSENT, not wrong-typed — an explicit `proposal: null` is+ * the documented "nothing to propose" answer and never lands here.*/export function validateReasonerResponse(parsed) {- const out = { analysis: '', proposal: null, pause: null };+ const out = { analysis: '', proposal: null, pause: null, invalidFields: [] };if (!parsed || typeof parsed !== 'object') return out;if (typeof parsed.analysis === 'string') out.analysis = parsed.analysis;+ else if (parsed.analysis != null) out.invalidFields.push('analysis');const p = parsed.proposal;if (p && typeof p === 'object' && !Array.isArray(p)) {@@ -43,6 +55,7 @@ export function validateReasonerResponse(parsed) {};}}+ if (p != null && !out.proposal) out.invalidFields.push('proposal');const pause = parsed.pause;if (pause && typeof pause === 'object' && !Array.isArray(pause)) {@@ -55,6 +68,8 @@ export function validateReasonerResponse(parsed) {out.pause = { blockOnIssue: isThis ? 'this' : num, reason };}}+ if (pause != null && !out.pause) out.invalidFields.push('pause');+return out;}