deckhearth/lib/flags/index.js

72 lines
2.3 KiB
JavaScript
Raw Normal View History

bootstrap: agent pipeline v0.5.0 + ship-readiness review Installs the three-layer agent-pipeline scaffold (https://github.com/varutasu/agent-pipeline @ v0.5.0): L1 — Context (curated brain) - AGENTS.md: orientation, conventions, 8 explicit gotchas - .cursor/rules/: no-go-zones, api-routes, auth-and-permissions, db-and-schema, ui-and-theming, schema-map - .cursor/skills/: add-api-route, add-page recipes - docs/agent-context/README.md: layer explainer - docs/SCHEMA_MAP.md: hand-curated Neon Postgres reference (replaces Prisma schema map since stack is raw SQL) L2 — Subagent roles (copied verbatim from upstream templates) - 9 .cursor/agents/role-*.md files: Conductor, IA-Architect, UX-Reviewer, Architect, Implementer, Reviewer, Design-System-Auditor, A11y-Auditor, Doc-Writer L3 — Pipeline scaffolding (Vercel variant) - CI: lint + schema-map-drift only (no duplicate build — Vercel handles it). Test job commented out until vitest lands. - preview-smoke + visual-diff via wait-for-vercel-preview - pr-health-rollup sticky comment aggregator - agent-context-drift weekly cron - PULL_REQUEST_TEMPLATE, CODEOWNERS (auth/admin paths tagged) - .convoys/ folder + seed ship-readiness.md review - lib/flags/index.js (JS — converted from TS template) - scripts/wt.sh (Cursor 3.2 deprecation stub), scripts/log-convoy-event.sh - tests/smoke/app.smoke.spec.ts (Playwright skeleton) Manifest - .agent-context-manifest.yml: tracks 31 artifacts by sha256 for future sync-agent-context drift detection Review - .convoys/ship-readiness.md: 16 findings (7 P0 ship-blockers, 5 P1 quality-bar, 4 P2 refactor, P3 UX/IA/a11y/docs) with proposed 13-convoy launch sequence. No production code changed in this commit. All findings in the ship-readiness review will be addressed in follow-up convoys starting with fix-auth-bypass. Structural brain: user-code-review-graph MCP has indexed the codebase (122 files, 628 nodes, 5602 edges, 11 communities, 84 flows). Per-developer; not committed. Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-23 00:16:08 -04:00
/**
* Feature flag wrapper. Lightweight, dependency-free, env-var driven.
*
* Usage:
* import { isEnabled } from '../../lib/flags';
* if (isEnabled('bookmark_count_badge', { userId: user?.userId })) {
* // ...
* }
*
* Flag values resolve from env vars: FLAG_<UPPER_SNAKE_NAME>=on|off|<percent>|<comma list of user ids>
* Examples:
* FLAG_BOOKMARK_COUNT_BADGE=on # everyone
* FLAG_BOOKMARK_COUNT_BADGE=off # nobody
* FLAG_BOOKMARK_COUNT_BADGE=10 # 10% of users (deterministic per userId)
* FLAG_BOOKMARK_COUNT_BADGE=u1,u2,u3 # specific user ids
*
* For a richer flag system (LaunchDarkly, Statsig, Unleash) replace the
* resolver below with an SDK call. The public API (isEnabled) stays the same.
*
* Pipeline integration: convoys with `skip: flag` in their frontmatter ship
* without flag-gating. Convoys without `skip: flag` MUST gate the new code
* behind a flag and document the rollout plan in the convoy file.
*/
const KNOWN_FLAGS = new Set([
// Add flags here as they're created. Helps catch typos.
// 'bookmark_count_badge',
]);
function envName(flag) {
return `FLAG_${flag.toUpperCase().replace(/[^A-Z0-9]/g, '_')}`;
}
function hashUserId(userId, salt) {
let h = 0;
const s = `${salt}::${userId}`;
for (let i = 0; i < s.length; i++) {
h = (h * 31 + s.charCodeAt(i)) | 0;
}
return Math.abs(h) % 100;
}
export function isEnabled(flag, ctx = {}) {
if (process.env.NODE_ENV !== 'test' && !KNOWN_FLAGS.has(flag)) {
if (typeof console !== 'undefined') {
console.warn(`[flags] unknown flag '${flag}'. Add it to KNOWN_FLAGS in lib/flags/index.js.`);
}
}
const raw = (ctx.envValue ?? process.env[envName(flag)] ?? 'off').trim().toLowerCase();
if (raw === 'on' || raw === 'true' || raw === '1') return true;
if (raw === 'off' || raw === 'false' || raw === '0' || raw === '') return false;
const pct = Number(raw);
if (!Number.isNaN(pct) && pct >= 0 && pct <= 100) {
if (!ctx.userId) return false;
return hashUserId(String(ctx.userId), flag) < pct;
}
if (raw.includes(',') || raw.length > 0) {
const ids = raw.split(',').map((s) => s.trim()).filter(Boolean);
return Boolean(ctx.userId && ids.includes(String(ctx.userId)));
}
return false;
}
export function listKnownFlags() {
return [...KNOWN_FLAGS].sort();
}