Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

feat(triage): add lifecycle detectors (stale + closed_externally)

diegosouzapw48c2d675fb8f
diff --git a/scripts/features/lib/lifecycle.mjs b/scripts/features/lib/lifecycle.mjs
new file mode 100644
index 0000000000..472d356291
--- /dev/null
+++ b/scripts/features/lib/lifecycle.mjs
@@ -0,0 +1,43 @@
+/**
+ * Lifecycle detectors: stale need_details, stale defer, closed-externally.
+ */
+
+export function isStaleNeedDetails(issue, thresholdDays, now = new Date()) {
+ const authorLogin = issue.author?.login;
+ let lastAuthorActivity = issue.createdAt;
+ for (const c of issue.comments ?? []) {
+ if (c.author?.login === authorLogin) {
+ if (new Date(c.createdAt).getTime() > new Date(lastAuthorActivity).getTime()) {
+ lastAuthorActivity = c.createdAt;
+ }
+ }
+ }
+ const daysSilent = Math.floor(
+ (now.getTime() - new Date(lastAuthorActivity).getTime()) / 86400_000
+ );
+ return {
+ stale: daysSilent >= thresholdDays,
+ daysSilent,
+ lastAuthorActivity,
+ };
+}
+
+export function isStaleDefer(meta, thresholdDays, now = new Date(), fallbackDate = null) {
+ const classifiedAt = meta?.snapshot?.classified_at || fallbackDate;
+ if (!classifiedAt) return { stale: false, daysInDefer: 0 };
+ const daysInDefer = Math.floor((now.getTime() - new Date(classifiedAt).getTime()) / 86400_000);
+ return { stale: daysInDefer >= thresholdDays, daysInDefer };
+}
+
+export function detectClosedExternally(issue, meta) {
+ const issueState = String(issue.state ?? "").toUpperCase();
+ const snapshotState = String(meta?.snapshot?.state ?? "open").toLowerCase();
+ if (issueState !== "CLOSED") return { flagged: false };
+ if (snapshotState === "closed") return { flagged: false };
+ const stateReason = issue.stateReason ?? null;
+ const out = { flagged: true, closedAt: issue.closedAt, stateReason };
+ if (stateReason === "COMPLETED") {
+ out.warningMessage = "possibly delivered by external work";
+ }
+ return out;
+}
diff --git a/tests/unit/feature-triage/lifecycle.test.mjs b/tests/unit/feature-triage/lifecycle.test.mjs
new file mode 100644
index 0000000000..dfb0dcf672
--- /dev/null
+++ b/tests/unit/feature-triage/lifecycle.test.mjs
@@ -0,0 +1,111 @@
+import { describe, it } from "node:test";
+import assert from "node:assert/strict";
+import {
+ isStaleNeedDetails,
+ isStaleDefer,
+ detectClosedExternally,
+} from "../../../scripts/features/lib/lifecycle.mjs";
+
+const now = new Date("2026-05-19T00:00:00Z");
+
+describe("isStaleNeedDetails", () => {
+ it("returns stale info when author silent ≥30 days", () => {
+ const issue = {
+ author: { login: "alice" },
+ createdAt: new Date(now.getTime() - 60 * 86400_000).toISOString(),
+ comments: [
+ {
+ author: { login: "alice" },
+ createdAt: new Date(now.getTime() - 40 * 86400_000).toISOString(),
+ },
+ {
+ author: { login: "bob" },
+ createdAt: new Date(now.getTime() - 5 * 86400_000).toISOString(),
+ },
+ ],
+ };
+ const r = isStaleNeedDetails(issue, 30, now);
+ assert.equal(r.stale, true);
+ assert.equal(r.daysSilent, 40);
+ assert.equal(r.lastAuthorActivity, issue.comments[0].createdAt);
+ });
+
+ it("returns not stale when author replied within 30 days", () => {
+ const issue = {
+ author: { login: "alice" },
+ createdAt: new Date(now.getTime() - 60 * 86400_000).toISOString(),
+ comments: [
+ {
+ author: { login: "alice" },
+ createdAt: new Date(now.getTime() - 10 * 86400_000).toISOString(),
+ },
+ ],
+ };
+ const r = isStaleNeedDetails(issue, 30, now);
+ assert.equal(r.stale, false);
+ });
+
+ it("uses createdAt when author never commented again", () => {
+ const issue = {
+ author: { login: "alice" },
+ createdAt: new Date(now.getTime() - 40 * 86400_000).toISOString(),
+ comments: [
+ {
+ author: { login: "bob" },
+ createdAt: new Date(now.getTime() - 5 * 86400_000).toISOString(),
+ },
+ ],
+ };
+ const r = isStaleNeedDetails(issue, 30, now);
+ assert.equal(r.stale, true);
+ assert.equal(r.daysSilent, 40);
+ });
+});
+
+describe("isStaleDefer", () => {
+ it("stale when classified_at ≥90 days ago", () => {
+ const meta = {
+ snapshot: { classified_at: new Date(now.getTime() - 100 * 86400_000).toISOString() },
+ };
+ const r = isStaleDefer(meta, 90, now);
+ assert.equal(r.stale, true);
+ assert.equal(r.daysInDefer, 100);
+ });
+ it("not stale when classified_at <90 days", () => {
+ const meta = {
+ snapshot: { classified_at: new Date(now.getTime() - 60 * 86400_000).toISOString() },
+ };
+ const r = isStaleDefer(meta, 90, now);
+ assert.equal(r.stale, false);
+ });
+ it("uses fallback mtime when classified_at missing", () => {
+ const meta = { snapshot: {} };
+ const fallback = new Date(now.getTime() - 100 * 86400_000);
+ const r = isStaleDefer(meta, 90, now, fallback);
+ assert.equal(r.stale, true);
+ });
+});
+
+describe("detectClosedExternally", () => {
+ it("flags as closed_externally when issue closed but snapshot says open", () => {
+ const issue = { state: "CLOSED", closedAt: "2026-05-01T00:00:00Z", stateReason: "NOT_PLANNED" };
+ const meta = { snapshot: { state: "open" } };
+ const r = detectClosedExternally(issue, meta);
+ assert.equal(r.flagged, true);
+ assert.equal(r.stateReason, "NOT_PLANNED");
+ });
+
+ it("emits delivery warning when COMPLETED reason", () => {
+ const issue = { state: "CLOSED", closedAt: "2026-05-01T00:00:00Z", stateReason: "COMPLETED" };
+ const meta = { snapshot: { state: "open" } };
+ const r = detectClosedExternally(issue, meta);
+ assert.equal(r.flagged, true);
+ assert.equal(r.warningMessage, "possibly delivered by external work");
+ });
+
+ it("returns flagged=false when issue still open", () => {
+ const issue = { state: "OPEN" };
+ const meta = { snapshot: { state: "open" } };
+ assert.equal(detectClosedExternally(issue, meta).flagged, false);
+ });
+});