@firstpick/pi-package-webui 0.6.8 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,108 @@
1
+ export const AUR_REVIEW_MAX_DISPLAY_PATH_LENGTH = 1024;
2
+ export const AUR_REVIEW_RPC_PAYLOAD_PREFIX = "AUR_REVIEW_RPC_PAYLOAD ";
3
+ export const AUR_REVIEW_RPC_PAYLOAD_TYPE = "firstpick.pi-extension-aur-review.review";
4
+ export const AUR_REVIEW_RPC_PAYLOAD_VERSION = 3;
5
+
6
+ const FINGERPRINT = /^[a-f0-9]{64}$/i;
7
+ const STAT_KEYS = ["files", "staged", "unstaged", "untracked", "deleted", "renamed", "unmerged"];
8
+
9
+ function hasOnlyKeys(value, keys) {
10
+ return !!value && typeof value === "object" && !Array.isArray(value) && Object.keys(value).every((key) => keys.includes(key));
11
+ }
12
+
13
+ function validTimestamp(value) {
14
+ if (typeof value !== "string" || !Number.isFinite(Date.parse(value))) return false;
15
+ try {
16
+ return new Date(value).toISOString() === value;
17
+ } catch {
18
+ return false;
19
+ }
20
+ }
21
+
22
+ export function aurReviewSafePath(value) {
23
+ const path = String(value || "");
24
+ if (!path || path.length > AUR_REVIEW_MAX_DISPLAY_PATH_LENGTH || path.includes("\0") || path.startsWith("/") || path.startsWith("\\")) return "";
25
+ const parts = path.split("/");
26
+ return parts.every((part) => part && part !== "." && part !== "..") ? path : "";
27
+ }
28
+
29
+ function changedFileStats(files) {
30
+ return files.reduce((stats, file) => ({
31
+ files: stats.files + 1,
32
+ staged: stats.staged + Number(file.staged),
33
+ unstaged: stats.unstaged + Number(file.unstaged),
34
+ untracked: stats.untracked + Number(file.untracked),
35
+ deleted: stats.deleted + Number(file.deleted),
36
+ renamed: stats.renamed + Number(file.renamed),
37
+ unmerged: stats.unmerged + Number(Boolean(file.unmerged)),
38
+ }), { files: 0, staged: 0, unstaged: 0, untracked: 0, deleted: 0, renamed: 0, unmerged: 0 });
39
+ }
40
+
41
+ function validChangedFile(file) {
42
+ return hasOnlyKeys(file, ["path", "oldPath", "indexStatus", "worktreeStatus", "staged", "unstaged", "untracked", "deleted", "renamed", "unmerged"])
43
+ && !!aurReviewSafePath(file.path)
44
+ && (file.oldPath === undefined || !!aurReviewSafePath(file.oldPath))
45
+ && typeof file.indexStatus === "string" && file.indexStatus.length === 1
46
+ && typeof file.worktreeStatus === "string" && file.worktreeStatus.length === 1
47
+ && typeof file.staged === "boolean"
48
+ && typeof file.unstaged === "boolean"
49
+ && typeof file.untracked === "boolean"
50
+ && typeof file.deleted === "boolean"
51
+ && typeof file.renamed === "boolean"
52
+ && (file.unmerged === undefined || typeof file.unmerged === "boolean");
53
+ }
54
+
55
+ function validStats(stats, files, total, truncated) {
56
+ if (!hasOnlyKeys(stats, STAT_KEYS)) return false;
57
+ if (!STAT_KEYS.every((key) => Number.isInteger(stats[key]) && stats[key] >= 0 && stats[key] <= total)) return false;
58
+ if (stats.files !== total) return false;
59
+ const stored = changedFileStats(files);
60
+ return STAT_KEYS.every((key) => truncated ? stored[key] <= stats[key] : stored[key] === stats[key]);
61
+ }
62
+
63
+ function validReport(report) {
64
+ return hasOnlyKeys(report, ["path", "size", "source"])
65
+ && !!aurReviewSafePath(report.path)
66
+ && Number.isFinite(report.size)
67
+ && report.size >= 0
68
+ && report.size <= 2 * 1024 * 1024
69
+ && ["explicit", "changed-file", "conventional"].includes(report.source);
70
+ }
71
+
72
+ /** Fail closed on the authorization-relevant durable-record relations. */
73
+ export function parseAurReviewPayload(lines, {
74
+ prefix = AUR_REVIEW_RPC_PAYLOAD_PREFIX,
75
+ type = AUR_REVIEW_RPC_PAYLOAD_TYPE,
76
+ version = AUR_REVIEW_RPC_PAYLOAD_VERSION,
77
+ } = {}) {
78
+ const raw = String(lines?.[0] || "").trim();
79
+ if (!raw.startsWith(prefix)) return null;
80
+ try {
81
+ const payload = JSON.parse(raw.slice(prefix.length));
82
+ if (!payload || payload.type !== type || payload.version !== version) return null;
83
+ if (typeof payload.repoRoot !== "string" || !payload.repoRoot || payload.repoRoot.includes("\0") || !/^(?:\/|[A-Za-z]:[\\/])/.test(payload.repoRoot) || !FINGERPRINT.test(String(payload.fingerprint || ""))) return null;
84
+ if (!((payload.scope === "working-tree" && payload.origin === "standalone") || (payload.scope === "staged" && payload.origin === "guided-git"))) return null;
85
+ const stagedPayload = payload.scope === "staged";
86
+ if (stagedPayload ? !FINGERPRINT.test(String(payload.stagedContentHash || "")) : payload.stagedContentHash !== undefined) return null;
87
+ if (!validTimestamp(payload.createdAt) || !validTimestamp(payload.updatedAt) || Date.parse(payload.createdAt) > Date.parse(payload.updatedAt)) return null;
88
+ if (!Number.isInteger(payload.changedFileTotal) || payload.changedFileTotal <= 0 || payload.changedFileTotal > 1_000_000) return null;
89
+ if (typeof payload.changedFilesTruncated !== "boolean" || !Array.isArray(payload.changedFiles) || payload.changedFiles.length > 500 || !payload.changedFiles.every(validChangedFile)) return null;
90
+ if (payload.changedFilesTruncated ? payload.changedFiles.length >= payload.changedFileTotal : payload.changedFiles.length !== payload.changedFileTotal) return null;
91
+ if (!validStats(payload.stats, payload.changedFiles, payload.changedFileTotal, payload.changedFilesTruncated)) return null;
92
+ if (!Array.isArray(payload.reports) || payload.reports.length > 20 || !payload.reports.every(validReport)) return null;
93
+ if (!hasOnlyKeys(payload.decision, ["state", "decidedAt", "reviewedFingerprint", "reviewedStagedContentHash"]) || !["pending", "approved", "declined", "closed"].includes(payload.decision.state)) return null;
94
+ const decision = payload.decision;
95
+ if (["approved", "declined"].includes(decision.state)) {
96
+ if (!validTimestamp(decision.decidedAt)
97
+ || decision.reviewedFingerprint !== payload.fingerprint
98
+ || (stagedPayload ? decision.reviewedStagedContentHash !== payload.stagedContentHash : decision.reviewedStagedContentHash !== undefined)) return null;
99
+ const decidedAt = Date.parse(decision.decidedAt);
100
+ if (decidedAt < Date.parse(payload.createdAt) || decidedAt > Date.parse(payload.updatedAt)) return null;
101
+ } else if (decision.decidedAt !== undefined || decision.reviewedFingerprint !== undefined || decision.reviewedStagedContentHash !== undefined) {
102
+ return null;
103
+ }
104
+ return payload;
105
+ } catch {
106
+ return null;
107
+ }
108
+ }
@@ -0,0 +1,34 @@
1
+ function commandBaseName(name) {
2
+ return String(name || "").replace(/:\d+$/, "");
3
+ }
4
+
5
+ function commandNameMatches(commandName, requestedName) {
6
+ const commandText = String(commandName || "");
7
+ const requested = String(requestedName || "");
8
+ return commandText === requested || (commandText.startsWith(`${requested}:`) && /^\d+$/.test(commandText.slice(requested.length + 1)));
9
+ }
10
+
11
+ /** Resolve only from one tab's already-normalized command catalog. */
12
+ export function resolveCommandForTabCatalog(catalog, name, { rpcOnly = false } = {}) {
13
+ const requested = String(name || "").trim();
14
+ if (!requested) return null;
15
+ const raw = Array.isArray(catalog?.raw) ? catalog.raw : [];
16
+ const available = Array.isArray(catalog?.available) ? catalog.available : raw;
17
+ const commands = (rpcOnly ? raw : available).filter((command) => !rpcOnly || command.source !== "native");
18
+ const exact = commands.find((command) => command.name === requested || command.invokeName === requested || command.duplicateNames?.includes(requested));
19
+ if (exact) return exact;
20
+ const canUseBaseAlias = available.some((command) => commandBaseName(command.name) === requested && command.invokeName && command.duplicateCount > 1);
21
+ return canUseBaseAlias ? commands.find((command) => commandNameMatches(command?.name, requested)) || null : null;
22
+ }
23
+
24
+ export function resolveRpcSlashCommandForTabCatalog(catalog, message) {
25
+ const text = String(message || "");
26
+ const match = text.match(/^\/([^\s]+)([\s\S]*)$/);
27
+ if (!match) return text;
28
+ const resolvedName = resolveCommandForTabCatalog(catalog, match[1], { rpcOnly: true })?.name || "";
29
+ return resolvedName && resolvedName !== match[1] ? `/${resolvedName}${match[2]}` : text;
30
+ }
31
+
32
+ export function guidedGitReviewAvailableForTabCatalog(catalog) {
33
+ return !!resolveCommandForTabCatalog(catalog, "aur-review", { rpcOnly: true });
34
+ }
@@ -0,0 +1,137 @@
1
+ const GUIDED_SCOPE = "staged";
2
+ const GUIDED_ORIGIN = "guided-git";
3
+ const FINGERPRINT = /^[a-f0-9]{64}$/i;
4
+ const RECOVERY_PROCESSES = new Set(["stage", "review"]);
5
+ const LATER_PROCESSES = new Set(["message", "commit", "push"]);
6
+
7
+ /** A decline is content-specific: retries require a different staged digest. */
8
+ export function guidedGitReviewCanRequestStagedContent(workflow, stagedContentHash) {
9
+ return !workflow?.guidedReviewDeclinedStagedContentHash
10
+ || workflow.guidedReviewDeclinedStagedContentHash !== stagedContentHash;
11
+ }
12
+
13
+ function validGuidedPayload(payload) {
14
+ return !!payload
15
+ && payload.scope === GUIDED_SCOPE
16
+ && payload.origin === GUIDED_ORIGIN
17
+ && typeof payload.repoRoot === "string"
18
+ && payload.repoRoot.length > 0
19
+ && FINGERPRINT.test(String(payload.fingerprint || ""))
20
+ && FINGERPRINT.test(String(payload.stagedContentHash || ""))
21
+ && typeof payload.updatedAt === "string"
22
+ && Number.isFinite(Date.parse(payload.updatedAt))
23
+ && payload.decision
24
+ && ["pending", "approved", "declined", "closed"].includes(payload.decision.state);
25
+ }
26
+
27
+ function sameReview(workflow, payload) {
28
+ return workflow.guidedReviewFingerprint === payload.fingerprint
29
+ && workflow.guidedReviewRepoRoot === payload.repoRoot
30
+ && workflow.guidedReviewStagedContentHash === payload.stagedContentHash;
31
+ }
32
+
33
+ export function guidedGitReviewHasApprovedBinding(workflow) {
34
+ return workflow?.guidedReviewStatus === "approved"
35
+ && typeof workflow.guidedReviewRepoRoot === "string"
36
+ && workflow.guidedReviewRepoRoot.length > 0
37
+ && FINGERPRINT.test(String(workflow.guidedReviewFingerprint || ""))
38
+ && FINGERPRINT.test(String(workflow.guidedReviewStagedContentHash || ""));
39
+ }
40
+
41
+ function isGuidedReviewGateActive(workflow) {
42
+ if (!workflow?.active) return false;
43
+ // A malformed or lost approved binding is not a legacy workflow. Keep it
44
+ // fail-closed so no later process can consume staged content without a new
45
+ // matching review.
46
+ if (workflow.guidedReviewStatus === "approved" && !guidedGitReviewHasApprovedBinding(workflow)) return true;
47
+ // The requirement remains after Stage/Review recovery, decline, close, or a
48
+ // cleared card. Only the matching approval transition may clear it.
49
+ if (workflow.guidedReviewRequired === true) return true;
50
+ return ["reviewRequesting", "review"].includes(workflow.step)
51
+ && ["requesting", "pending"].includes(workflow.guidedReviewStatus);
52
+ }
53
+
54
+ /**
55
+ * Recovery stays available while a staged review gate is active, but later
56
+ * process navigation cannot bypass an outstanding review decision.
57
+ */
58
+ export function guidedGitReviewProcessNavigationAllowed(workflow, process) {
59
+ if (!isGuidedReviewGateActive(workflow)) return true;
60
+ if (RECOVERY_PROCESSES.has(process)) return true;
61
+ return !LATER_PROCESSES.has(process);
62
+ }
63
+
64
+ function clearedGuidedGitReviewPatch(workflow, guidedReviewRequired) {
65
+ return {
66
+ guidedReviewStatus: "",
67
+ guidedReviewFingerprint: "",
68
+ guidedReviewRepoRoot: "",
69
+ guidedReviewStagedContentHash: "",
70
+ guidedReviewRequestedAt: 0,
71
+ guidedReviewDeclinedStagedContentHash: workflow?.guidedReviewDeclinedStagedContentHash || "",
72
+ guidedReviewRequired,
73
+ };
74
+ }
75
+
76
+ /**
77
+ * Return the review-state patch for a process selection, or null when the
78
+ * requested process is blocked. Later process navigation retains the exact
79
+ * approved binding; recovery deliberately clears it and requires a new review.
80
+ */
81
+ export function guidedGitReviewProcessSelectionPatch(workflow, process) {
82
+ if (!guidedGitReviewProcessNavigationAllowed(workflow, process)) return null;
83
+ const reviewWasRequired = workflow?.guidedReviewRequired === true || workflow?.guidedReviewStatus === "approved";
84
+ if (RECOVERY_PROCESSES.has(process)) return clearedGuidedGitReviewPatch(workflow, reviewWasRequired);
85
+ if (guidedGitReviewHasApprovedBinding(workflow)) {
86
+ return {
87
+ guidedReviewStatus: workflow.guidedReviewStatus,
88
+ guidedReviewFingerprint: workflow.guidedReviewFingerprint,
89
+ guidedReviewRepoRoot: workflow.guidedReviewRepoRoot,
90
+ guidedReviewStagedContentHash: workflow.guidedReviewStagedContentHash,
91
+ guidedReviewRequestedAt: workflow.guidedReviewRequestedAt || 0,
92
+ guidedReviewDeclinedStagedContentHash: workflow.guidedReviewDeclinedStagedContentHash || "",
93
+ guidedReviewRequired: false,
94
+ };
95
+ }
96
+ return clearedGuidedGitReviewPatch(workflow, false);
97
+ }
98
+
99
+ /** A cleared matching pending widget is equivalent to an explicit close. */
100
+ export function guidedGitReviewWidgetRemovalTransition(workflow) {
101
+ return workflow?.active
102
+ && workflow.step === "review"
103
+ && workflow.guidedReviewStatus === "pending"
104
+ && typeof workflow.guidedReviewFingerprint === "string"
105
+ && workflow.guidedReviewFingerprint.length > 0
106
+ && typeof workflow.guidedReviewRepoRoot === "string"
107
+ && workflow.guidedReviewRepoRoot.length > 0
108
+ && FINGERPRINT.test(String(workflow.guidedReviewStagedContentHash || ""))
109
+ ? "closed"
110
+ : "ignore";
111
+ }
112
+
113
+ /**
114
+ * Return the only valid Guided Git transition for an already schema-validated
115
+ * browser payload. Keeping this pure makes approval gating independently testable.
116
+ */
117
+ export function guidedGitReviewTransition(workflow, payload) {
118
+ if (!workflow?.active || !["reviewRequesting", "review"].includes(workflow.step) || !validGuidedPayload(payload)) return "ignore";
119
+ const decision = payload.decision.state;
120
+ const requestedAt = Number(workflow.guidedReviewRequestedAt || 0);
121
+ const isFreshRequestPayload = workflow.guidedReviewStatus === "requesting"
122
+ && (!requestedAt || Date.parse(payload.updatedAt) >= requestedAt);
123
+ if (decision === "pending") {
124
+ if (isFreshRequestPayload) return "pending";
125
+ return workflow.guidedReviewStatus === "pending" && sameReview(workflow, payload) ? "pending" : "ignore";
126
+ }
127
+ if (decision === "closed") {
128
+ if (isFreshRequestPayload) return "closed";
129
+ return workflow.guidedReviewStatus === "pending" && sameReview(workflow, payload) ? "closed" : "ignore";
130
+ }
131
+ if (!["approved", "declined"].includes(decision) || workflow.guidedReviewStatus !== "pending" || !sameReview(workflow, payload)) return "ignore";
132
+ if (payload.decision.reviewedFingerprint !== payload.fingerprint
133
+ || payload.decision.reviewedStagedContentHash !== payload.stagedContentHash
134
+ || typeof payload.decision.decidedAt !== "string"
135
+ || !Number.isFinite(Date.parse(payload.decision.decidedAt))) return "ignore";
136
+ return decision;
137
+ }