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

fix: prune malformed tracker labels (#12579)

[._.]/ Adam Eivye98a731ed793
diff --git a/server/services/autonomousJobs/selfDiagnostics.js b/server/services/autonomousJobs/selfDiagnostics.js
index 28bfc16702..c3de1f37b4 100644
--- a/server/services/autonomousJobs/selfDiagnostics.js
+++ b/server/services/autonomousJobs/selfDiagnostics.js
@@ -203,6 +203,68 @@ export async function ensureDiagnosticsLabels({ cwd = PATHS.root, exec = runCli
}
}
+const MALFORMED_LABEL = /^\S+ (?:--color )?#?[0-9A-Fa-f]{6}(?: .*)?$/
+
+/**
+ * Remove labels accidentally created by passing a whole "name color description"
+ * string as one CLI argument. A label is deleted only after both issue and PR
+ * searches succeed and report no usage; any ambiguous read is fail-closed.
+ */
+export async function pruneMalformedLabels({ cwd = PATHS.root, exec = runCli } = {}) {
+ const { code, stdout, stderr } = await exec('gh', ['label', 'list', '--limit', '500', '--json', 'name'], { cwd })
+ if (code !== 0) {
+ console.warn(`⚠️ Self-diagnostics: could not list labels for pruning: ${stderr.trim()}`)
+ return { pruned: 0, skipped: 0 }
+ }
+
+ const labels = safeJSONParse(stdout, null, { logError: false })
+ if (!Array.isArray(labels) || labels.some(label => typeof label?.name !== 'string')) {
+ console.warn('⚠️ Self-diagnostics: could not parse labels for pruning')
+ return { pruned: 0, skipped: 0 }
+ }
+
+ let pruned = 0
+ let skipped = 0
+ for (const { name } of labels) {
+ if (!MALFORMED_LABEL.test(name)) continue
+
+ const usage = await Promise.all([
+ exec('gh', ['issue', 'list', '--state', 'all', '--label', name, '--limit', '1', '--json', 'number'], { cwd }),
+ exec('gh', ['pr', 'list', '--state', 'all', '--label', name, '--limit', '1', '--json', 'number'], { cwd })
+ ])
+ const [issues, pullRequests] = usage
+ const issueNumbers = issues.code === 0 ? safeJSONParse(issues.stdout, null, { logError: false }) : null
+ const prNumbers = pullRequests.code === 0 ? safeJSONParse(pullRequests.stdout, null, { logError: false }) : null
+ if (!Array.isArray(issueNumbers) || !Array.isArray(prNumbers) ||
+ issueNumbers.some(item => !Number.isInteger(item?.number)) ||
+ prNumbers.some(item => !Number.isInteger(item?.number))) {
+ skipped++
+ console.warn(`⚠️ Self-diagnostics: could not verify usage for malformed label ${JSON.stringify(name)}; leaving it in place`)
+ continue
+ }
+ if (issueNumbers.length || prNumbers.length) {
+ skipped++
+ const usages = [
+ ...issueNumbers.map(item => `issue #${item.number}`),
+ ...prNumbers.map(item => `PR #${item.number}`)
+ ]
+ console.warn(`⚠️ Self-diagnostics: malformed label ${JSON.stringify(name)} is in use by ${usages.join(', ')}; leaving it in place`)
+ continue
+ }
+
+ const deleted = await exec('gh', ['label', 'delete', name, '--yes'], { cwd })
+ if (deleted.code !== 0) {
+ skipped++
+ console.warn(`⚠️ Self-diagnostics: failed to delete malformed label ${JSON.stringify(name)}: ${deleted.stderr.trim()}`)
+ continue
+ }
+ pruned++
+ }
+
+ console.log(`🧹 Pruned ${pruned} malformed labels`)
+ return { pruned, skipped }
+}
+
/**
* Self-diagnostics handler (SCRIPT_HANDLERS['self-diagnostics']).
*
@@ -263,13 +325,14 @@ export async function runSelfDiagnostics({
const editArgs = ['issue', 'edit', String(found.issue.number), '--body', body]
const addingLabel = hasFailures && !found.issue.labels.includes(NEEDS_ATTENTION_LABEL)
if (addingLabel) {
+ await ensureDiagnosticsLabels({ cwd, exec })
editArgs.push('--add-label', NEEDS_ATTENTION_LABEL)
} else if (!hasFailures && found.issue.labels.includes(NEEDS_ATTENTION_LABEL)) {
editArgs.push('--remove-label', NEEDS_ATTENTION_LABEL)
}
+ await pruneMalformedLabels({ cwd, exec })
// Only pre-create labels when we're about to attach one (gh needs it to exist);
// a body-only or remove-label edit doesn't (the label already exists then).
- if (addingLabel) await ensureDiagnosticsLabels({ cwd, exec })
const { code, stderr } = await exec('gh', editArgs, { cwd })
if (code !== 0) {
console.warn(`⚠️ Self-diagnostics: failed to update issue #${found.issue.number}: ${stderr.trim()}`)
@@ -281,11 +344,13 @@ export async function runSelfDiagnostics({
if (!hasFailures) {
// Nothing failing and no existing summary — don't create noise.
+ await pruneMalformedLabels({ cwd, exec })
console.log('🩺 Self-diagnostics: no failures and no existing summary issue — nothing to file')
return { failingCount: 0, categories: [], issue: null }
}
await ensureDiagnosticsLabels({ cwd, exec })
+ await pruneMalformedLabels({ cwd, exec })
const createArgs = ['issue', 'create', '--title', title, '--body', body, '--label', MONITORING_LABEL, '--label', NEEDS_ATTENTION_LABEL]
const { code, stdout, stderr } = await exec('gh', createArgs, { cwd })
if (code !== 0) {
diff --git a/server/services/autonomousJobs/selfDiagnostics.test.js b/server/services/autonomousJobs/selfDiagnostics.test.js
index 32311d52e6..08be263979 100644
--- a/server/services/autonomousJobs/selfDiagnostics.test.js
+++ b/server/services/autonomousJobs/selfDiagnostics.test.js
@@ -6,6 +6,7 @@ import {
buildDiagnosticsIssueTitle,
buildDiagnosticsIssueBody,
findMonitoringIssue,
+ pruneMalformedLabels,
runSelfDiagnostics,
MONITORING_LABEL,
NEEDS_ATTENTION_LABEL,
@@ -233,6 +234,68 @@ describe('findMonitoringIssue', () => {
})
})
+describe('pruneMalformedLabels', () => {
+ it('deletes only unreferenced malformed names and leaves legitimate spaced labels alone', async () => {
+ const exec = makeExec([
+ { match: argsHave('label', 'list'), result: { code: 0, stdout: JSON.stringify([
+ { name: 'good first issue' },
+ { name: 'help wanted' },
+ { name: 'model:light D4C5F9' },
+ { name: 'effort:medium --color 76C7C7' },
+ { name: 'security 0366D6 Proposed from a security audit' }
+ ]) } },
+ { match: argsHave('issue', 'list'), result: { code: 0, stdout: '[]' } },
+ { match: argsHave('pr', 'list'), result: { code: 0, stdout: '[]' } },
+ { match: argsHave('label', 'delete'), result: { code: 0, stdout: '' } }
+ ])
+
+ const result = await pruneMalformedLabels({ exec })
+ expect(result).toEqual({ pruned: 3, skipped: 0 })
+ const deleted = exec.mock.calls.filter(([, args]) => args.includes('delete')).map(([, args]) => args[2])
+ expect(deleted).toEqual(['model:light D4C5F9', 'effort:medium --color 76C7C7', 'security 0366D6 Proposed from a security audit'])
+ })
+
+ it('keeps malformed labels used by either an issue or a pull request', async () => {
+ const exec = makeExec([
+ { match: argsHave('label', 'list'), result: { code: 0, stdout: JSON.stringify([
+ { name: 'model:light D4C5F9' },
+ { name: 'effort:medium 76C7C7' }
+ ]) } },
+ { match: argsHave('issue', 'list', 'model:light D4C5F9'), result: { code: 0, stdout: '[{"number":12}]' } },
+ { match: argsHave('issue', 'list'), result: { code: 0, stdout: '[]' } },
+ { match: argsHave('pr', 'list', 'model:light D4C5F9'), result: { code: 0, stdout: '[]' } },
+ { match: argsHave('pr', 'list', 'effort:medium 76C7C7'), result: { code: 0, stdout: '[{"number":34}]' } },
+ { match: argsHave('pr', 'list'), result: { code: 0, stdout: '[]' } },
+ { match: argsHave('label', 'delete'), result: { code: 0, stdout: '' } }
+ ])
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
+
+ const result = await pruneMalformedLabels({ exec })
+ expect(result).toEqual({ pruned: 0, skipped: 2 })
+ expect(exec.mock.calls.some(([, args]) => args.includes('delete'))).toBe(false)
+ expect(warn).toHaveBeenCalledTimes(2)
+ expect(warn.mock.calls.map(([line]) => line)).toEqual(expect.arrayContaining([
+ expect.stringContaining('issue #12'),
+ expect.stringContaining('PR #34')
+ ]))
+ warn.mockRestore()
+ })
+
+ it('fails closed when either usage check fails', async () => {
+ const exec = makeExec([
+ { match: argsHave('label', 'list'), result: { code: 0, stdout: '[{"name":"model:light D4C5F9"}]' } },
+ { match: argsHave('issue', 'list'), result: { code: 0, stdout: '[]' } },
+ { match: argsHave('pr', 'list'), result: { code: 1, stdout: '', stderr: 'unavailable' } }
+ ])
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
+
+ const result = await pruneMalformedLabels({ exec })
+ expect(result).toEqual({ pruned: 0, skipped: 1 })
+ expect(exec.mock.calls.some(([, args]) => args.includes('delete'))).toBe(false)
+ warn.mockRestore()
+ })
+})
+
describe('runSelfDiagnostics', () => {
const loadLearning = async () => ({ byTaskType: sampleMetrics })