feat(cron): support prefix matching for job IDs

Allow short ID prefixes to match cron jobs, similar to how Git
handles short commit hashes. If the prefix uniquely matches one
job, it resolves to that job. If multiple jobs match, an error
lists the ambiguous matches.

This removes the need to copy full UUIDs when interacting with
cron jobs via the tool interface.
This commit is contained in:
andrewdamelio 2026-02-13 13:12:08 +00:00
parent 94763cd87d
commit cf76d23535

View File

@ -47,7 +47,20 @@ function assertDeliverySupport(job: Pick<CronJob, "sessionTarget" | "delivery">)
}
export function findJobOrThrow(state: CronServiceState, id: string) {
const job = state.store?.jobs.find((j) => j.id === id);
// Exact match first
let job = state.store?.jobs.find((j) => j.id === id);
if (!job) {
// Prefix match (like git short hashes)
const matches = state.store?.jobs.filter((j) => j.id.startsWith(id)) ?? [];
if (matches.length === 1) {
job = matches[0];
} else if (matches.length > 1) {
const ids = matches.map((j) => j.id).join(", ");
throw new Error(
`ambiguous cron job id prefix "${id}" matches ${matches.length} jobs: ${ids}`,
);
}
}
if (!job) {
throw new Error(`unknown cron job id: ${id}`);
}