Skip to content
Closed
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
22 changes: 19 additions & 3 deletions packages/gittensory-engine/src/governor-ledger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { isDeepStrictEqual } from "node:util";

/** Immutable governor decision vocabulary — unknown values fail closed before insert. */
export const GOVERNOR_LEDGER_EVENT_TYPES = Object.freeze([
"allowed",
Expand Down Expand Up @@ -31,6 +29,24 @@ export type NormalizedGovernorLedgerEvent = {
const governorEventTypeSet = new Set<string>(GOVERNOR_LEDGER_EVENT_TYPES);

/* v8 ignore start -- Normalization helpers are covered through normalizeGovernorLedgerEvent export tests. */
// Self-contained structural-equality check (no node:util) so this package stays runtime-portable across the
// Worker backend and the Node-only miner CLI. Scoped to serializePayload's own round-trip-fidelity use: both
// sides here are always plain objects/arrays/primitives (one is a fresh JSON.parse result), so this never needs
// to handle Maps, Dates, RegExps, or prototypes the way a general-purpose deep-equal would.
function deepStrictEqual(a: unknown, b: unknown): boolean {
if (Object.is(a, b)) return true;
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
if (Array.isArray(a) !== Array.isArray(b)) return false;
const aRecord = a as Record<string, unknown>;
const bRecord = b as Record<string, unknown>;
const aKeys = Object.keys(aRecord);
const bKeys = Object.keys(bRecord);
if (aKeys.length !== bKeys.length) return false;
return aKeys.every(
(key) => Object.prototype.hasOwnProperty.call(bRecord, key) && deepStrictEqual(aRecord[key], bRecord[key]),
);
}

function normalizeRequiredString(value: unknown, code: string): string {
if (typeof value !== "string") throw new Error(code);
const trimmed = value.trim();
Expand All @@ -57,7 +73,7 @@ function serializePayload(payload: unknown): string {
} catch {
throw new Error("invalid_payload");
}
if (!isDeepStrictEqual(JSON.parse(json), payload)) {
if (!deepStrictEqual(JSON.parse(json), payload)) {
throw new Error("invalid_payload");
}
return json;
Expand Down
1 change: 1 addition & 0 deletions packages/gittensory-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export {
computeLaneFit,
type GoalModelInput,
} from "./goal-model.js";
export {
classifyContributorFit,
type ContributorFit,
type ContributorFitCheck,
Expand Down
10 changes: 8 additions & 2 deletions packages/gittensory-engine/src/opportunity-freshness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ function pickTimestamp(issue: FreshnessIssue): string | null {
return null;
}

// No usable timestamp survived pickTimestamp's updatedAt->createdAt fallback -- an unknown age must never
// register as "just updated" (age 0, the freshest possible score). Floor it to a large sentinel so
// computeOpportunityFreshness's exponential decay clamps straight to the 0.05 floor, matching how a genuinely
// stale issue scores, not a fresh one.
const UNKNOWN_AGE_DAYS = 9999;

function issueAgeDays(value: string | null, nowMs: number): number {
if (!value) return 0;
if (!value) return UNKNOWN_AGE_DAYS;
const parsed = Date.parse(value);
if (!Number.isFinite(parsed)) return 0;
if (!Number.isFinite(parsed)) return UNKNOWN_AGE_DAYS;
return Math.floor((nowMs - parsed) / 86_400_000);
}

Expand Down