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
18 changes: 17 additions & 1 deletion pm-stack/dispatch/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,17 @@ export interface SessionRequest {
wait: boolean;
}

/** Minimum spec floor for a session prompt with no explicit title (#4925):
* a prompt shorter than this cannot name any work, so accepting it would mint
* a spec-less repo issue that costs a triage lane. 40 chars is the value the
* stalled #4948 carried; an explicit title exempts the request (the caller
* named the work). */
export const MIN_PROMPT_SPEC_CHARS = 40;

export function parseSessionRequest(body: unknown): SessionRequest {
if (typeof body !== "object" || body === null) throw new ValidationError("body: must be a JSON object");
const b = body as Record<string, unknown>;
return {
const req: SessionRequest = {
repo: reqString(b, "repo"),
prompt: reqString(b, "prompt"),
title: optNonBlankString(b, "title"),
Expand All @@ -157,6 +164,15 @@ export function parseSessionRequest(body: unknown): SessionRequest {
issue: optBool(b, "issue", true),
wait: optBool(b, "wait", true),
};
// Minimum-spec gate (#4925). It runs after every field validation above so
// existing per-field errors keep precedence; an explicit title is the
// caller naming the work, so it exempts the request.
if (req.title === undefined && req.prompt.length < MIN_PROMPT_SPEC_CHARS) {
throw new ValidationError(
`prompt: too short to name any work (${req.prompt.length} < ${MIN_PROMPT_SPEC_CHARS} chars) and no title given — describe the work in the prompt or pass an explicit title`,
);
}
return req;
}

/** Session-directory scoping for POST /v1/sessions (issues #2903, #2664).
Expand Down
2 changes: 1 addition & 1 deletion pm-stack/dispatch/test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function sessionsRequest(token: string, body: Record<string, unknown>): Request
// (the exact orphan class #4917 tracks; baseline runs of this file
// filed #4937/#4938). Issue-creation and failure-compensation behaviour
// is covered hermetically by launch_failure.test.ts stubs.
const base = { repo: "developing-today/code", prompt: "do the thing", issue: false };
const base = { repo: "developing-today/code", prompt: "fix the pm-dispatch session request minimum spec gate", issue: false };

describe("POST /v1/sessions directory scoping (issue #2903)", () => {
test("OIDC + foreign directory is rejected with 400 before any side effect", async () => {
Expand Down
29 changes: 25 additions & 4 deletions pm-stack/dispatch/test/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { mkdtempSync, mkdirSync, realpathSync, rmSync, symlinkSync } from "node:
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
MIN_PROMPT_SPEC_CHARS,
ValidationError,
parseModel,
parseSessionRequest,
Expand Down Expand Up @@ -149,7 +150,10 @@ describe("request validation", () => {
}
});
test("sessions require repo and prompt; defaults issue+wait on", () => {
const r = parseSessionRequest({ repo: "developing-today/code", prompt: "do the thing" });
const r = parseSessionRequest({
repo: "developing-today/code",
prompt: "Fix the parser so trailing whitespace in a mark is rejected",
});
expect(r.issue).toBe(true);
expect(r.wait).toBe(true);
expect(r.priority).toBe(40);
Expand All @@ -158,6 +162,21 @@ describe("request validation", () => {
expect(() => parseSessionRequest("nope")).toThrow(ValidationError);
});

test("minimum spec gate rejects a titleless prompt that names no work (#4925)", () => {
const repo = "developing-today/code";
// Too short and no title: refused, and the error names the missing spec.
expect(() => parseSessionRequest({ repo, prompt: "do the thing" })).toThrow(/too short to name any work/);
expect(() => parseSessionRequest({ repo, prompt: "x".repeat(MIN_PROMPT_SPEC_CHARS - 1) })).toThrow(ValidationError);
// An explicit title exempts the request (the caller named the work).
const titled = parseSessionRequest({ repo, prompt: "do the thing", title: "Fix pm-dispatch spec gate" });
expect(titled.title).toBe("Fix pm-dispatch spec gate");
// A prompt at the floor with no title passes.
const compliant = parseSessionRequest({ repo, prompt: "x".repeat(MIN_PROMPT_SPEC_CHARS) });
expect(compliant.title).toBeUndefined();
// The gate runs after field validation: an invalid priority still errors as priority.
expect(() => parseSessionRequest({ repo, prompt: "x", priority: "constructor" })).toThrow(/priority/);
});

test("a prototype-spelling priority 400s at parse time, so no non-number reaches the packet (#2757)", () => {
expect(() => parseSessionRequest({ repo: "developing-today/code", prompt: "x", priority: "constructor" })).toThrow(
ValidationError,
Expand All @@ -172,7 +191,9 @@ describe("request validation", () => {
parseTaskRequest({ repo: "developing-today/code", title: "t", prompt: "p", priority: "__proto__" }),
).toThrow(ValidationError);
// A valid named priority still parses to a number.
expect(parseSessionRequest({ repo: "developing-today/code", prompt: "x", priority: "high" }).priority).toBe(20);
expect(
parseSessionRequest({ repo: "developing-today/code", prompt: "x", title: "t", priority: "high" }).priority,
).toBe(20);
});

test("tasks accept prompt or body, default kind triage and issue off", () => {
Expand Down Expand Up @@ -301,15 +322,15 @@ describe("session blank title/directory parsing (issue #2664)", () => {
test("empty and whitespace-only title/directory parse as omitted", () => {
const r = parseSessionRequest({
repo: "developing-today/code",
prompt: "do the thing",
prompt: "make the mark parser reject trailing whitespace",
title: " ",
directory: "",
});
expect(r.title).toBeUndefined();
expect(r.directory).toBeUndefined();
const r2 = parseSessionRequest({
repo: "developing-today/code",
prompt: "do the thing",
prompt: "make the mark parser reject trailing whitespace",
title: "",
directory: " \t ",
});
Expand Down
Loading