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

refactor([voice-code-agent-target-managed-app]): table-drive appResolver tiers + reverse-prefix test

[._.]/ Adam Eivy4120e696f2b2
diff --git a/server/lib/appResolver.js b/server/lib/appResolver.js
index 1c830eb7f2..bbdb232469 100644
--- a/server/lib/appResolver.js
+++ b/server/lib/appResolver.js
@@ -1,48 +1,45 @@
-// Fuzzy match a spoken/typed phrase ("BookLoom", "book loom", "the bookloom
-// app") to a managed app from `getActiveApps()`. Pure helper — caller fetches
-// the apps list.
-//
-// Tiered match strategy mirrors `navManifest.resolveNavCommand`: exact →
-// prefix → substring, biased to the longest candidate name on substring ties
-// so "book loom" picks "BookLoom" over a stray "Book" app.
-//
-// Returns the matched app object (with `id`) or `null` when no candidate is
-// close enough. Caller decides the not-found UX.
+// Sibling of `navManifest.resolveNavCommand` but for managed apps (free-form
+// `{ id, name }` records, not a static alias table). Tier ordering matters:
+// an exact match must beat a prefix/substring overlap even when a later
+// entry would also qualify on the looser tier, so the tiers run as ordered
+// passes rather than one short-circuit loop. Longest-name tiebreak biases
+// "book loom" to "BookLoom" over a stray "Book" app.
-const normalize = (s) => (typeof s === 'string' ? s : '')
+const normalizeForMatch = (s) => (typeof s === 'string' ? s : '')
.toLowerCase()
.replace(/[^a-z0-9]+/g, '');
+// Per-tier predicates. The 3-char floor on candidate name/id keeps a 2-char id
+// (e.g. "ai") from greedily matching every phrase under prefix/substring; the
+// `minTarget` floor on the substring tier blocks short targets from finding
+// long candidates by inclusion. Tier 1 carries neither floor — an exact hit
+// is unambiguous regardless of length.
+const TIERS = [
+ { test: (e, t) => e.name === t || e.id === t },
+ { test: (e, t) =>
+ (e.name.length >= 3 && (e.name.startsWith(t) || t.startsWith(e.name)))
+ || (e.id.length >= 3 && (e.id.startsWith(t) || t.startsWith(e.id))) },
+ { minTarget: 3, test: (e, t) =>
+ (e.name.length >= 3 && (e.name.includes(t) || t.includes(e.name)))
+ || (e.id.length >= 3 && (e.id.includes(t) || t.includes(e.id))) },
+];
+
+const pickLongest = (hits) => hits.reduce((a, b) => (b.name.length >= a.name.length ? b : a)).app;
+
export function resolveAppByPhrase(phrase, apps) {
- const target = normalize(phrase);
+ const target = normalizeForMatch(phrase);
if (target.length < 2) return null;
if (!Array.isArray(apps) || apps.length === 0) return null;
const entries = apps
- .map((app) => ({ app, name: normalize(app?.name), id: normalize(app?.id) }))
+ .map((app) => ({ app, name: normalizeForMatch(app?.name), id: normalizeForMatch(app?.id) }))
.filter((e) => e.name || e.id);
if (entries.length === 0) return null;
- const pickLongest = (hits) => hits.reduce((a, b) => (b.name.length >= a.name.length ? b : a)).app;
-
- // Tier 1: exact id or normalized-name match.
- for (const e of entries) {
- if (e.name === target || e.id === target) return e.app;
- }
- // Tier 2: prefix overlap in either direction. Min 3 chars on the candidate
- // so a 2-letter id (e.g. "ai") doesn't greedily prefix-match every utterance.
- const prefixHits = entries.filter((e) =>
- (e.name.length >= 3 && (e.name.startsWith(target) || target.startsWith(e.name)))
- || (e.id.length >= 3 && (e.id.startsWith(target) || target.startsWith(e.id))));
- if (prefixHits.length) return pickLongest(prefixHits);
-
- // Tier 3: substring containment in either direction, min 3 chars on both
- // sides so "ax" doesn't match every app whose name contains "ax".
- if (target.length >= 3) {
- const substringHits = entries.filter((e) =>
- (e.name.length >= 3 && (e.name.includes(target) || target.includes(e.name)))
- || (e.id.length >= 3 && (e.id.includes(target) || target.includes(e.id))));
- if (substringHits.length) return pickLongest(substringHits);
+ for (const { minTarget = 0, test } of TIERS) {
+ if (target.length < minTarget) continue;
+ const hits = entries.filter((e) => test(e, target));
+ if (hits.length) return pickLongest(hits);
}
return null;
}
diff --git a/server/lib/appResolver.test.js b/server/lib/appResolver.test.js
index 0d86518c75..c56e4f1cd5 100644
--- a/server/lib/appResolver.test.js
+++ b/server/lib/appResolver.test.js
@@ -48,6 +48,13 @@ describe('resolveAppByPhrase', () => {
expect(resolveAppByPhrase('aim higher', APPS)).toBeNull();
});
+ it('reverse-prefix matches when the target is longer than a candidate id/name', () => {
+ // Spoken phrases occasionally pick up a stray suffix — "bookloom abc"
+ // should still resolve to BookLoom, not 404.
+ expect(resolveAppByPhrase('bookloom-abc-extra', APPS)?.id).toBe('bookloom-abc');
+ expect(resolveAppByPhrase('finance-tracker-xyz-extra', APPS)?.id).toBe('finance-tracker-xyz');
+ });
+
it('picks the longest candidate on substring overlap', () => {
const apps = [
{ id: 'book', name: 'Book' },
diff --git a/server/services/voice/tools.js b/server/services/voice/tools.js
index b913fa02b8..7e7adf735f 100644
--- a/server/services/voice/tools.js
+++ b/server/services/voice/tools.js
@@ -1842,11 +1842,8 @@ const TOOLS = [
return { ok: false, error: 'code-agent disabled', summary: 'Coding-agent dispatch is off — turn it on under Settings, Voice, Coding agent.' };
}
- // Optional spoken target: fuzzy-resolve "in BookLoom" to a managed-app
- // id so the spawner runs the agent in that app's workspace (cos.js
- // `metadata.app` → agentLifecycle prepareAgentWorkspace). Reject an
- // explicit-but-unknown target rather than silently falling through to
- // the PortOS workspace, so the user hears "I don't see that app."
+ // Reject an explicit-but-unknown app rather than silently falling
+ // through to the PortOS workspace — that's the whole point of asking.
let resolvedAppId = null;
let resolvedAppName = null;
if (appPhrase) {