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

address review (codex): attribute dialogue by beat position, not canon order

Adam Eivyc4acbabc1daf
diff --git a/server/lib/editorial/dialogue.js b/server/lib/editorial/dialogue.js
index 655eaf32e8..e1e7586045 100644
--- a/server/lib/editorial/dialogue.js
+++ b/server/lib/editorial/dialogue.js
@@ -347,9 +347,17 @@ export function attributeDialogueByOwner(text, owners) {
const beat = para.replace(QUOTED_SPAN_RE, ' ').replace(/\s+/g, ' ').trim();
let owner = null;
if (beat) {
+ // Credit the owner whose name appears EARLIEST in the beat — the speaker.
+ // In "…," Aria told Bram. the leftmost name (Aria) is the speaker; picking
+ // by list (canon) order instead would make attribution depend on canon
+ // ordering and credit a beat that merely mentions a second character to
+ // the wrong owner. Ties (same index — impossible for distinct names, but
+ // guarded) keep first-listed.
+ let bestIndex = Infinity;
for (const o of list) {
o.matcher.lastIndex = 0; // non-global, but reset defensively
- if (o.matcher.test(beat)) { owner = o.key; break; }
+ const m2 = o.matcher.exec(beat);
+ if (m2 && m2.index < bestIndex) { bestIndex = m2.index; owner = o.key; }
}
}
if (owner) {
diff --git a/server/lib/editorial/dialogue.test.js b/server/lib/editorial/dialogue.test.js
index edfda7b7de..ff1d2a8ba3 100644
--- a/server/lib/editorial/dialogue.test.js
+++ b/server/lib/editorial/dialogue.test.js
@@ -233,11 +233,15 @@ describe('attributeDialogueByOwner', () => {
expect(byOwner.get('aria')).toBe(1);
});
- it('first-owner-wins when a beat names two characters', () => {
+ it('credits the earliest-named character in the beat, independent of owner (canon) order', () => {
+ // "Aria told Bram" — Aria is the speaker (leftmost name). The owner list is
+ // ordered [bram, aria] (canon order) to prove attribution follows beat
+ // POSITION, not list order: a position-blind first-in-list scan would
+ // wrongly credit Bram.
const text = '"Stop," Aria told Bram.';
const { byOwner } = attributeDialogueByOwner(text, [
- owner('aria', 'Aria'),
owner('bram', 'Bram'),
+ owner('aria', 'Aria'),
]);
expect(byOwner.get('aria')).toBe(1);
expect(byOwner.has('bram')).toBe(false);