Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions pm-stack/aggregator/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,21 +286,25 @@ function answerQuestionIds(action: Pick<NewAction, "verb" | "payload">): string[

const SESSION_SOURCES = new Set(["opencode-sandbox", "opencode-laptop", "claude-code", "cron"]);

function cleanText(v: unknown, max: number): string | undefined {
if (typeof v !== "string") return undefined;
const t = v.trim();
if (t === "") return undefined;
// Cap `t` at `max` UTF-16 code units, but never leave half of a surrogate
// pair at the cut (#7694, mirroring the #3178 guard in pm-dispatch
// validate.ts): an astral character straddling the cap is dropped whole.
function capUnits(t: string, max: number): string {
if (t.length <= max) return t;
// Keep the UTF-16 cap, but never leave half of a surrogate pair at the
// cut (#7694, mirroring the #3178 guard in pm-dispatch validate.ts):
// an astral character straddling the cap is dropped whole.
let end = max;
const before = t.charCodeAt(end - 1);
const after = t.charCodeAt(end);
if (before >= 0xd800 && before <= 0xdbff && after >= 0xdc00 && after <= 0xdfff) end--;
return t.slice(0, end);
}

function cleanText(v: unknown, max: number): string | undefined {
if (typeof v !== "string") return undefined;
const t = v.trim();
if (t === "") return undefined;
return capUnits(t, max);
}

/** Validate an untrusted POST body into a NewAction, or explain why not. */
export function validateNewAction(body: unknown): { ok: true; value: NewAction } | { ok: false; error: string } {
if (typeof body !== "object" || body === null) return { ok: false, error: "body must be a JSON object" };
Expand Down Expand Up @@ -794,8 +798,11 @@ export class ActionsStore {
if (a.state !== "new") return { action: a, transitioned: false };
a.state = result.outcome;
a.appliedAt = new Date().toISOString();
a.appliedByRun = result.byRun.slice(0, 200);
if (result.note) a.resultNote = result.note.slice(0, 2_000);
// Same surrogate-safe cap as cleanText (#7711): a raw slice here stored
// a lone high surrogate in appliedByRun/resultNote whenever an astral
// character straddled the cap.
a.appliedByRun = capUnits(result.byRun, 200);
if (result.note) a.resultNote = capUnits(result.note, 2_000);
await this.persist();
return { action: a, transitioned: true };
}
Expand Down
28 changes: 28 additions & 0 deletions pm-stack/aggregator/test/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,34 @@ describe("dashboard actions store", () => {
expect(retry?.action.state).toBe("applied");
});

test("markResult caps never split a surrogate pair (#7711)", async () => {
const dir = mkdtempSync(join(tmpdir(), "agg-actions-result-surrogate-"));
const store = new ActionsStore(join(dir, "actions.json"));
// byRun cap 200: 199 units + an astral character straddling the cap is
// dropped whole, never stored as a lone high surrogate.
const a1 = await store.add(newAction({ objectRef: "orch:item-result-byRun" }));
const byRun = await store.markResult(a1.action.id, {
outcome: "applied",
byRun: "r".repeat(199) + "\u{1F600}" + "tail",
});
expect(byRun?.appliedByRun?.length).toBe(199);
expect(byRun?.appliedByRun?.endsWith("r")).toBe(true);
// note cap 2000: same rule at the larger cap.
const a2 = await store.add(newAction({ objectRef: "orch:item-result-note" }));
const noted = await store.markResult(a2.action.id, {
outcome: "applied",
byRun: "orch-test",
note: "n".repeat(1999) + "\u{1F600}" + "tail",
});
expect(noted?.resultNote?.length).toBe(1999);
expect(noted?.resultNote?.endsWith("n")).toBe(true);
// A pair that fits exactly inside the cap is kept whole.
const a3 = await store.add(newAction({ objectRef: "orch:item-result-fit" }));
const fit = "n".repeat(1998) + "\u{1F600}";
const fitted = await store.markResult(a3.action.id, { outcome: "applied", byRun: "orch-test", note: fit });
expect(fitted?.resultNote).toBe(fit);
});

test("answer question slots dedupe option and survey submissions with an audit pointer", async () => {
const dir = mkdtempSync(join(tmpdir(), "agg-answer-dedupe-"));
const store = new ActionsStore(join(dir, "actions.json"));
Expand Down
Loading