@granular-software/fractory 0.0.0-stage → 0.1.25
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.
- package/LICENSE +4 -0
- package/README.md +28 -2
- package/dist/activity-volume.d.ts +18 -0
- package/dist/activity-volume.js +94 -0
- package/dist/annotations.d.ts +122 -0
- package/dist/annotations.js +304 -0
- package/dist/benchmark-cli.d.ts +1 -0
- package/dist/benchmark-cli.js +95 -0
- package/dist/benchmark-worker.d.ts +2 -0
- package/dist/benchmark-worker.js +22 -0
- package/dist/benchmarks.d.ts +19 -0
- package/dist/benchmarks.js +414 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +63 -0
- package/dist/block-cli.d.ts +1 -0
- package/dist/block-cli.js +88 -0
- package/dist/block-documents.d.ts +35 -0
- package/dist/block-documents.js +84 -0
- package/dist/change-impact.d.ts +55 -0
- package/dist/change-impact.js +223 -0
- package/dist/check-source.d.ts +2 -0
- package/dist/check-source.js +66 -0
- package/dist/checks-model.d.ts +64 -0
- package/dist/checks-model.js +36 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +359 -0
- package/dist/code-model.d.ts +38 -0
- package/dist/code-model.js +94 -0
- package/dist/code-store.d.ts +9 -0
- package/dist/code-store.js +79 -0
- package/dist/compare-artifact.d.ts +6 -0
- package/dist/compare-artifact.js +277 -0
- package/dist/compare.d.ts +81 -0
- package/dist/compare.js +291 -0
- package/dist/compiler.d.ts +10 -0
- package/dist/compiler.js +1240 -0
- package/dist/compose.d.ts +9 -0
- package/dist/compose.js +72 -0
- package/dist/core.d.ts +87 -0
- package/dist/core.js +207 -0
- package/dist/coverage-cli.d.ts +1 -0
- package/dist/coverage-cli.js +42 -0
- package/dist/declaration-sources.d.ts +3 -0
- package/dist/declaration-sources.js +51 -0
- package/dist/declared-graph.d.ts +3 -0
- package/dist/declared-graph.js +67 -0
- package/dist/display-name.d.ts +2 -0
- package/dist/display-name.js +8 -0
- package/dist/error-heat.d.ts +58 -0
- package/dist/error-heat.js +96 -0
- package/dist/evidence.d.ts +3 -0
- package/dist/evidence.js +68 -0
- package/dist/factory-graph.d.ts +7 -0
- package/dist/factory-graph.js +389 -0
- package/dist/factory-model.d.ts +111 -0
- package/dist/factory-model.js +1 -0
- package/dist/git-code.d.ts +47 -0
- package/dist/git-code.js +352 -0
- package/dist/git-refs.d.ts +4 -0
- package/dist/git-refs.js +37 -0
- package/dist/history-activity.d.ts +4 -0
- package/dist/history-activity.js +129 -0
- package/dist/history-churn.d.ts +36 -0
- package/dist/history-churn.js +302 -0
- package/dist/history-git.d.ts +21 -0
- package/dist/history-git.js +125 -0
- package/dist/history-ledger.d.ts +43 -0
- package/dist/history-ledger.js +450 -0
- package/dist/history-ownership.d.ts +10 -0
- package/dist/history-ownership.js +75 -0
- package/dist/history-source.d.ts +13 -0
- package/dist/history-source.js +100 -0
- package/dist/integrations-cli.d.ts +2 -0
- package/dist/integrations-cli.js +159 -0
- package/dist/interface-map.d.ts +15 -0
- package/dist/interface-map.js +125 -0
- package/dist/issue-cache.d.ts +9 -0
- package/dist/issue-cache.js +68 -0
- package/dist/issue-cli.d.ts +1 -0
- package/dist/issue-cli.js +89 -0
- package/dist/issue-links.d.ts +15 -0
- package/dist/issue-links.js +125 -0
- package/dist/linear-issues.d.ts +19 -0
- package/dist/linear-issues.js +124 -0
- package/dist/local-runtime.d.ts +8 -0
- package/dist/local-runtime.js +59 -0
- package/dist/map-activity.d.ts +73 -0
- package/dist/map-activity.js +234 -0
- package/dist/map-model.d.ts +5 -0
- package/dist/map-model.js +166 -0
- package/dist/map-quality.d.ts +20 -0
- package/dist/map-quality.js +61 -0
- package/dist/mapping-coverage.d.ts +87 -0
- package/dist/mapping-coverage.js +404 -0
- package/dist/model.d.ts +186 -0
- package/dist/model.js +66 -0
- package/dist/operation-benchmark-worker.d.ts +2 -0
- package/dist/operation-benchmark-worker.js +60 -0
- package/dist/operation-benchmark.d.ts +29 -0
- package/dist/operation-benchmark.js +1 -0
- package/dist/otel-metrics.d.ts +21 -0
- package/dist/otel-metrics.js +146 -0
- package/dist/performance-model.d.ts +185 -0
- package/dist/performance-model.js +396 -0
- package/dist/product-map.d.ts +71 -0
- package/dist/product-map.js +197 -0
- package/dist/provider-access.cjs +207 -0
- package/dist/provider-access.d.cts +14 -0
- package/dist/public-boundaries.d.ts +13 -0
- package/dist/public-boundaries.js +115 -0
- package/dist/reports-cli.d.ts +1 -0
- package/dist/reports-cli.js +111 -0
- package/dist/reports-model.d.ts +27 -0
- package/dist/reports-model.js +39 -0
- package/dist/reports.d.ts +23 -0
- package/dist/reports.js +223 -0
- package/dist/repository-map.d.ts +22 -0
- package/dist/repository-map.js +153 -0
- package/dist/resource-cache.d.ts +17 -0
- package/dist/resource-cache.js +65 -0
- package/dist/review-cli.d.ts +19 -0
- package/dist/review-cli.js +224 -0
- package/dist/review.d.ts +48 -0
- package/dist/review.js +289 -0
- package/dist/runtime-metrics.d.ts +26 -0
- package/dist/runtime-metrics.js +290 -0
- package/dist/scope.d.ts +14 -0
- package/dist/scope.js +168 -0
- package/dist/sentry-activity.d.ts +39 -0
- package/dist/sentry-activity.js +174 -0
- package/dist/sentry.d.ts +130 -0
- package/dist/sentry.js +369 -0
- package/dist/service-release.d.ts +16 -0
- package/dist/service-release.js +14 -0
- package/dist/setup-cli.d.ts +2 -0
- package/dist/setup-cli.js +52 -0
- package/dist/setup-kit.cjs +163 -0
- package/dist/setup-kit.d.cts +32 -0
- package/dist/source-index.d.ts +37 -0
- package/dist/source-index.js +170 -0
- package/dist/source-review-git.d.ts +7 -0
- package/dist/source-review-git.js +105 -0
- package/dist/source-review.d.ts +18 -0
- package/dist/source-review.js +573 -0
- package/dist/telemetry.d.ts +17 -0
- package/dist/telemetry.js +51 -0
- package/dist/time-window.d.ts +9 -0
- package/dist/time-window.js +35 -0
- package/dist/trace-evidence.d.ts +23 -0
- package/dist/trace-evidence.js +55 -0
- package/dist/trace-model.d.ts +26 -0
- package/dist/trace-model.js +1 -0
- package/dist/tracing.d.ts +44 -0
- package/dist/tracing.js +123 -0
- package/dist/work-check.d.ts +18 -0
- package/dist/work-check.js +115 -0
- package/dist/work-cli.d.ts +1 -0
- package/dist/work-cli.js +176 -0
- package/dist/work-context.d.ts +59 -0
- package/dist/work-context.js +594 -0
- package/dist/work-intents.d.ts +6 -0
- package/dist/work-intents.js +140 -0
- package/dist/work-items.d.ts +58 -0
- package/dist/work-items.js +86 -0
- package/dist/work-lock-check.d.ts +12 -0
- package/dist/work-lock-check.js +339 -0
- package/dist/work-locks.d.ts +10 -0
- package/dist/work-locks.js +98 -0
- package/dist/work-model.d.ts +119 -0
- package/dist/work-model.js +310 -0
- package/dist/work-review-git.d.ts +8 -0
- package/dist/work-review-git.js +111 -0
- package/dist/work-review.d.ts +26 -0
- package/dist/work-review.js +426 -0
- package/dist/work-switcher-model.d.ts +43 -0
- package/dist/work-switcher-model.js +79 -0
- package/dist/work-switcher.d.ts +6 -0
- package/dist/work-switcher.js +171 -0
- package/dist/working-cli.d.ts +1 -0
- package/dist/working-cli.js +33 -0
- package/dist/working-code.d.ts +74 -0
- package/dist/working-code.js +421 -0
- package/dist/workspace-notes.d.ts +71 -0
- package/dist/workspace-notes.js +177 -0
- package/dist/workspace-tests.d.ts +21 -0
- package/dist/workspace-tests.js +573 -0
- package/package.json +547 -4
- package/src/activity-volume.ts +149 -0
- package/src/annotations.ts +485 -0
- package/src/benchmark-cli.ts +118 -0
- package/src/benchmark-worker.ts +22 -0
- package/src/benchmarks.ts +493 -0
- package/src/bin.ts +54 -0
- package/src/block-cli.ts +106 -0
- package/src/block-documents.ts +117 -0
- package/src/change-impact.ts +280 -0
- package/src/check-source.ts +82 -0
- package/src/checks-model.ts +108 -0
- package/src/cli.ts +440 -0
- package/src/code-model.ts +159 -0
- package/src/code-store.ts +98 -0
- package/src/compare-artifact.ts +314 -0
- package/src/compare.ts +440 -0
- package/src/compiler.ts +1512 -0
- package/src/compose.ts +94 -0
- package/src/core.ts +361 -0
- package/src/coverage-cli.ts +52 -0
- package/src/declaration-sources.ts +69 -0
- package/src/declared-graph.ts +75 -0
- package/src/display-name.ts +8 -0
- package/src/error-heat.ts +161 -0
- package/src/evidence.ts +94 -0
- package/src/factory-graph.ts +462 -0
- package/src/factory-model.ts +82 -0
- package/src/git-code.ts +461 -0
- package/src/git-refs.ts +43 -0
- package/src/history-activity.ts +146 -0
- package/src/history-churn.ts +416 -0
- package/src/history-git.ts +163 -0
- package/src/history-ledger.ts +636 -0
- package/src/history-ownership.ts +116 -0
- package/src/history-source.ts +117 -0
- package/src/integrations-cli.ts +187 -0
- package/src/interface-map.ts +166 -0
- package/src/issue-cache.ts +96 -0
- package/src/issue-cli.ts +137 -0
- package/src/issue-links.ts +153 -0
- package/src/linear-issues.ts +154 -0
- package/src/local-runtime.ts +70 -0
- package/src/map-activity.ts +309 -0
- package/src/map-model.ts +213 -0
- package/src/map-quality.ts +66 -0
- package/src/mapping-coverage.ts +570 -0
- package/src/model.ts +228 -0
- package/src/operation-benchmark-worker.ts +60 -0
- package/src/operation-benchmark.ts +28 -0
- package/src/otel-metrics.ts +176 -0
- package/src/performance-model.ts +575 -0
- package/src/product-map.ts +271 -0
- package/src/provider-access.cjs +207 -0
- package/src/provider-access.d.cts +14 -0
- package/src/public-boundaries.ts +143 -0
- package/src/reports-cli.ts +125 -0
- package/src/reports-model.ts +73 -0
- package/src/reports.ts +276 -0
- package/src/repository-map.ts +191 -0
- package/src/resource-cache.ts +76 -0
- package/src/review-cli.ts +256 -0
- package/src/review.ts +376 -0
- package/src/runtime-metrics.ts +386 -0
- package/src/scope.ts +192 -0
- package/src/sentry-activity.ts +234 -0
- package/src/sentry.ts +545 -0
- package/src/service-release.ts +18 -0
- package/src/setup-cli.ts +58 -0
- package/src/setup-kit.cjs +163 -0
- package/src/setup-kit.d.cts +32 -0
- package/src/source-index.ts +208 -0
- package/src/source-review-git.ts +131 -0
- package/src/source-review.ts +736 -0
- package/src/telemetry.ts +68 -0
- package/src/time-window.ts +51 -0
- package/src/trace-evidence.ts +80 -0
- package/src/trace-model.ts +26 -0
- package/src/tracing.ts +165 -0
- package/src/work-check.ts +149 -0
- package/src/work-cli.ts +229 -0
- package/src/work-context.ts +753 -0
- package/src/work-intents.ts +196 -0
- package/src/work-items.ts +151 -0
- package/src/work-lock-check.ts +549 -0
- package/src/work-locks.ts +108 -0
- package/src/work-model.ts +466 -0
- package/src/work-review-git.ts +130 -0
- package/src/work-review.ts +573 -0
- package/src/work-switcher-model.ts +121 -0
- package/src/work-switcher.ts +191 -0
- package/src/working-cli.ts +38 -0
- package/src/working-code.ts +582 -0
- package/src/workspace-notes.ts +203 -0
- package/src/workspace-tests.ts +654 -0
- package/toolkit/GUIDE.md +354 -0
- package/toolkit/SKILL.md +142 -0
- package/toolkit/bin/fractory +2 -0
- package/toolkit/build.json +1 -0
package/src/issue-cli.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { join, resolve } from "node:path";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { readIssueLinks, saveIssueLink } from "./issue-links";
|
|
4
|
+
import { readIssueCache, recordIssues } from "./issue-cache";
|
|
5
|
+
import { normalizeLinearIssue, LinearIssuesReader } from "./linear-issues";
|
|
6
|
+
import { workItems, issueReference } from "./work-items";
|
|
7
|
+
export async function runIssues(args: string[]) {
|
|
8
|
+
const option = (flag: string) => {
|
|
9
|
+
const index = args.indexOf(flag);
|
|
10
|
+
return index < 0 ? undefined : args[index + 1];
|
|
11
|
+
};
|
|
12
|
+
const root = resolve(option("--root") || process.cwd()),
|
|
13
|
+
file = join(root, "fractal.issues.json"),
|
|
14
|
+
cache = join(root, ".fractal/linear-issues.json");
|
|
15
|
+
const { registry, revision } = readIssueLinks(file);
|
|
16
|
+
if (args.includes("--help"))
|
|
17
|
+
return "fractal issues [list|link|unlink|import|sync] [--root path]\nlink --issue GRA-123 --block <id> [--block <id> ...] --graph graph.json\nunlink --issue GRA-123\nimport --file verified-linear-export.json\nsync uses FRACTAL_LINEAR_API_KEY or LINEAR_API_KEY (read-only).";
|
|
18
|
+
const command = args[1] || "list";
|
|
19
|
+
if (command === "link" || command === "unlink") {
|
|
20
|
+
const key = issueReference(option("--issue") || "").key;
|
|
21
|
+
const blocks = args.flatMap((arg, index) =>
|
|
22
|
+
arg === "--block" ? [args[index + 1]] : [],
|
|
23
|
+
);
|
|
24
|
+
let known = new Set<string>();
|
|
25
|
+
if (command === "link") {
|
|
26
|
+
const graph = JSON.parse(
|
|
27
|
+
readFileSync(
|
|
28
|
+
resolve(
|
|
29
|
+
root,
|
|
30
|
+
option("--graph") || "fractory.json",
|
|
31
|
+
),
|
|
32
|
+
"utf8",
|
|
33
|
+
),
|
|
34
|
+
);
|
|
35
|
+
known = new Set([
|
|
36
|
+
graph.id,
|
|
37
|
+
...graph.elements.map((block: { id: string }) => block.id),
|
|
38
|
+
]);
|
|
39
|
+
if (!blocks.length) throw Error("Choose at least one --block");
|
|
40
|
+
}
|
|
41
|
+
const metadata = registry.workspace?.id
|
|
42
|
+
? readIssueCache(cache, registry.workspace.id).find(
|
|
43
|
+
(issue) => issue.id === key || issue.identifier === key,
|
|
44
|
+
)
|
|
45
|
+
: undefined;
|
|
46
|
+
const prior = registry.links.find(
|
|
47
|
+
(link) =>
|
|
48
|
+
link.issue === key ||
|
|
49
|
+
link.identifier === key ||
|
|
50
|
+
link.issue === metadata?.id,
|
|
51
|
+
);
|
|
52
|
+
const ref = issueReference(option("--issue")!);
|
|
53
|
+
if (
|
|
54
|
+
ref.workspace &&
|
|
55
|
+
registry.workspace?.url &&
|
|
56
|
+
new URL(registry.workspace.url).pathname !== "/" + ref.workspace
|
|
57
|
+
)
|
|
58
|
+
throw Error("Issue belongs to another workspace");
|
|
59
|
+
if (command === "unlink" && !prior) throw Error("Issue has no saved links");
|
|
60
|
+
return JSON.stringify(
|
|
61
|
+
saveIssueLink(
|
|
62
|
+
file,
|
|
63
|
+
revision,
|
|
64
|
+
{
|
|
65
|
+
issue: metadata?.id || prior?.issue || key,
|
|
66
|
+
identifier: metadata?.identifier || prior?.identifier,
|
|
67
|
+
blocks: command === "unlink" ? [] : blocks,
|
|
68
|
+
},
|
|
69
|
+
known,
|
|
70
|
+
),
|
|
71
|
+
null,
|
|
72
|
+
2,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
if (command === "import" || command === "sync") {
|
|
76
|
+
if (!registry.workspace?.id)
|
|
77
|
+
throw Error("Set workspace.id in fractal.issues.json first");
|
|
78
|
+
let issues;
|
|
79
|
+
if (command === "import") {
|
|
80
|
+
const data = JSON.parse(
|
|
81
|
+
readFileSync(resolve(root, option("--file") || ""), "utf8"),
|
|
82
|
+
);
|
|
83
|
+
if (
|
|
84
|
+
data.workspaceId !== registry.workspace.id ||
|
|
85
|
+
!Array.isArray(data.issues) ||
|
|
86
|
+
data.issues.length > 500
|
|
87
|
+
)
|
|
88
|
+
throw Error(
|
|
89
|
+
"Import must match the configured workspace and contain up to 500 issues",
|
|
90
|
+
);
|
|
91
|
+
issues = data.issues.map((issue: any) => {
|
|
92
|
+
const checkedAt = issue.checkedAt || data.checkedAt;
|
|
93
|
+
if (typeof checkedAt !== "string")
|
|
94
|
+
throw Error(
|
|
95
|
+
"Include the actual checkedAt timestamp from the Linear read",
|
|
96
|
+
);
|
|
97
|
+
return normalizeLinearIssue(issue, checkedAt);
|
|
98
|
+
});
|
|
99
|
+
for (const issue of issues)
|
|
100
|
+
if (
|
|
101
|
+
new URL(issue.url).pathname.split("/")[1] !==
|
|
102
|
+
new URL(registry.workspace.url).pathname.split("/")[1]
|
|
103
|
+
)
|
|
104
|
+
throw Error("Imported issue belongs to a different workspace");
|
|
105
|
+
} else {
|
|
106
|
+
const token =
|
|
107
|
+
process.env.FRACTAL_LINEAR_API_KEY || process.env.LINEAR_API_KEY;
|
|
108
|
+
if (!token)
|
|
109
|
+
throw Error(
|
|
110
|
+
"Configure a read-only Linear credential to refresh status",
|
|
111
|
+
);
|
|
112
|
+
issues = await new LinearIssuesReader(
|
|
113
|
+
token,
|
|
114
|
+
registry.workspace.id,
|
|
115
|
+
).issues(registry.links.map((link) => link.issue));
|
|
116
|
+
}
|
|
117
|
+
recordIssues(cache, registry.workspace.id, issues);
|
|
118
|
+
return `Recorded ${issues.length} issue statuses; no Linear issues were modified.`;
|
|
119
|
+
}
|
|
120
|
+
if (command !== "list")
|
|
121
|
+
throw Error("Unknown issues command. Use fractal issues --help");
|
|
122
|
+
const items = workItems(
|
|
123
|
+
registry,
|
|
124
|
+
registry.workspace?.id ? readIssueCache(cache, registry.workspace.id) : [],
|
|
125
|
+
);
|
|
126
|
+
return JSON.stringify(
|
|
127
|
+
{
|
|
128
|
+
revision,
|
|
129
|
+
workspace: registry.workspace,
|
|
130
|
+
items: option("--block")
|
|
131
|
+
? items.filter((item) => item.blocks.includes(option("--block")!))
|
|
132
|
+
: items,
|
|
133
|
+
},
|
|
134
|
+
null,
|
|
135
|
+
2,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/** Tiny Git-versionable link registry; Linear retains ownership of issue metadata/status. */
|
|
2
|
+
import {
|
|
3
|
+
readFileSync,
|
|
4
|
+
writeFileSync,
|
|
5
|
+
renameSync,
|
|
6
|
+
unlinkSync,
|
|
7
|
+
lstatSync,
|
|
8
|
+
mkdirSync,
|
|
9
|
+
} from "node:fs";
|
|
10
|
+
import { dirname } from "node:path";
|
|
11
|
+
import { createHash } from "node:crypto";
|
|
12
|
+
import { issueReference, type IssueLinks } from "./work-items";
|
|
13
|
+
const empty: IssueLinks = { schema: "fractal.issues.v1", links: [] };
|
|
14
|
+
export class IssueLinkConflict extends Error {}
|
|
15
|
+
export function readIssueLinks(file: string): {
|
|
16
|
+
registry: IssueLinks;
|
|
17
|
+
revision: string;
|
|
18
|
+
} {
|
|
19
|
+
let raw: string;
|
|
20
|
+
try {
|
|
21
|
+
if (lstatSync(file).isSymbolicLink())
|
|
22
|
+
throw Error("Issue link registry must not be a symlink");
|
|
23
|
+
raw = readFileSync(file, "utf8");
|
|
24
|
+
} catch (error: any) {
|
|
25
|
+
if (error.code !== "ENOENT") throw error;
|
|
26
|
+
raw = JSON.stringify(empty);
|
|
27
|
+
}
|
|
28
|
+
if (raw.length > 256_000)
|
|
29
|
+
throw Error("Issue link registry exceeds its size limit");
|
|
30
|
+
const value = JSON.parse(raw) as IssueLinks;
|
|
31
|
+
if (
|
|
32
|
+
value.schema !== "fractal.issues.v1" ||
|
|
33
|
+
!Array.isArray(value.links) ||
|
|
34
|
+
value.links.length > 500
|
|
35
|
+
)
|
|
36
|
+
throw Error("Invalid issue link registry");
|
|
37
|
+
const ids = new Set<string>();
|
|
38
|
+
if (value.workspace) {
|
|
39
|
+
const url = new URL(value.workspace.url);
|
|
40
|
+
if (
|
|
41
|
+
url.origin !== "https://linear.app" ||
|
|
42
|
+
url.username ||
|
|
43
|
+
url.password ||
|
|
44
|
+
url.search ||
|
|
45
|
+
url.hash ||
|
|
46
|
+
url.pathname.split("/").filter(Boolean).length !== 1
|
|
47
|
+
)
|
|
48
|
+
throw Error("Invalid Linear workspace URL");
|
|
49
|
+
}
|
|
50
|
+
for (const link of value.links) {
|
|
51
|
+
const key = issueReference(link.issue).key;
|
|
52
|
+
if (
|
|
53
|
+
key !== link.issue ||
|
|
54
|
+
ids.has(key) ||
|
|
55
|
+
!Array.isArray(link.blocks) ||
|
|
56
|
+
!link.blocks.length ||
|
|
57
|
+
link.blocks.length > 64 ||
|
|
58
|
+
link.blocks.some((b) => typeof b !== "string" || !b || b.length > 400) ||
|
|
59
|
+
new Set(link.blocks).size !== link.blocks.length
|
|
60
|
+
)
|
|
61
|
+
throw Error("Invalid or duplicate issue link");
|
|
62
|
+
if (
|
|
63
|
+
link.identifier &&
|
|
64
|
+
issueReference(link.identifier).key !== link.identifier
|
|
65
|
+
)
|
|
66
|
+
throw Error("Invalid issue identifier");
|
|
67
|
+
for (const alias of new Set([
|
|
68
|
+
key,
|
|
69
|
+
...(link.identifier ? [link.identifier] : []),
|
|
70
|
+
])) {
|
|
71
|
+
if (ids.has(alias)) throw Error("Duplicate issue alias");
|
|
72
|
+
ids.add(alias);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
registry: value,
|
|
77
|
+
revision: createHash("sha256").update(raw).digest("hex"),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
export function saveIssueLink(
|
|
81
|
+
file: string,
|
|
82
|
+
expectedRevision: string,
|
|
83
|
+
input: { issue: string; identifier?: string; blocks: string[] },
|
|
84
|
+
knownBlocks: ReadonlySet<string>,
|
|
85
|
+
) {
|
|
86
|
+
if (!/^[a-f0-9]{64}$/.test(expectedRevision))
|
|
87
|
+
throw new IssueLinkConflict("Reload issue links before saving");
|
|
88
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
89
|
+
const lock = file + ".lock";
|
|
90
|
+
let owned = false;
|
|
91
|
+
try {
|
|
92
|
+
try {
|
|
93
|
+
writeFileSync(lock, "", { flag: "wx", mode: 0o600 });
|
|
94
|
+
owned = true;
|
|
95
|
+
} catch (error: any) {
|
|
96
|
+
if (error.code === "EEXIST")
|
|
97
|
+
throw new IssueLinkConflict(
|
|
98
|
+
"Another writer holds the issue-link lock; retry when it finishes",
|
|
99
|
+
);
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
const current = readIssueLinks(file);
|
|
103
|
+
if (current.revision !== expectedRevision)
|
|
104
|
+
throw new IssueLinkConflict(
|
|
105
|
+
"Issue links changed. Reload before saving so another edit is not overwritten",
|
|
106
|
+
);
|
|
107
|
+
const key = issueReference(input.issue).key,
|
|
108
|
+
identifier = input.identifier
|
|
109
|
+
? issueReference(input.identifier).key
|
|
110
|
+
: undefined;
|
|
111
|
+
if (
|
|
112
|
+
!Array.isArray(input.blocks) ||
|
|
113
|
+
input.blocks.length > 64 ||
|
|
114
|
+
input.blocks.some((b) => typeof b !== "string")
|
|
115
|
+
)
|
|
116
|
+
throw Error("Choose up to 64 blocks");
|
|
117
|
+
const prior = current.registry.links.find(
|
|
118
|
+
(l) =>
|
|
119
|
+
l.issue === key ||
|
|
120
|
+
l.identifier === key ||
|
|
121
|
+
(identifier && (l.identifier === identifier || l.issue === identifier)),
|
|
122
|
+
);
|
|
123
|
+
const blocks = [...new Set(input.blocks)].sort();
|
|
124
|
+
for (const block of blocks)
|
|
125
|
+
if (!knownBlocks.has(block) && !prior?.blocks.includes(block))
|
|
126
|
+
throw Error("Unknown block: " + block);
|
|
127
|
+
const links = current.registry.links.filter((l) => l !== prior);
|
|
128
|
+
const savedKey = prior?.identifier === key ? prior.issue : key;
|
|
129
|
+
const savedIdentifier = identifier || prior?.identifier;
|
|
130
|
+
if (blocks.length)
|
|
131
|
+
links.push({
|
|
132
|
+
issue: savedKey,
|
|
133
|
+
...(savedIdentifier ? { identifier: savedIdentifier } : {}),
|
|
134
|
+
blocks,
|
|
135
|
+
});
|
|
136
|
+
const value: IssueLinks = {
|
|
137
|
+
...current.registry,
|
|
138
|
+
links: links.sort((a, b) =>
|
|
139
|
+
(a.identifier || a.issue).localeCompare(b.identifier || b.issue),
|
|
140
|
+
),
|
|
141
|
+
};
|
|
142
|
+
writeFileSync(lock, JSON.stringify(value, null, 2) + "\n");
|
|
143
|
+
readIssueLinks(lock);
|
|
144
|
+
renameSync(lock, file);
|
|
145
|
+
owned = false;
|
|
146
|
+
return readIssueLinks(file);
|
|
147
|
+
} finally {
|
|
148
|
+
if (owned)
|
|
149
|
+
try {
|
|
150
|
+
unlinkSync(lock);
|
|
151
|
+
} catch {}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { issueReference, type LinearWorkIssue } from "./work-items";
|
|
2
|
+
export function normalizeLinearIssue(
|
|
3
|
+
raw: any,
|
|
4
|
+
checkedAt = new Date().toISOString(),
|
|
5
|
+
): LinearWorkIssue {
|
|
6
|
+
const id = raw.uuid || raw.id,
|
|
7
|
+
identifier = raw.identifier || raw.id;
|
|
8
|
+
if (
|
|
9
|
+
!/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i.test(id || "") ||
|
|
10
|
+
!/^[A-Z][A-Z0-9]{0,15}-[1-9][0-9]{0,9}$/.test(identifier || "") ||
|
|
11
|
+
typeof raw.title !== "string" ||
|
|
12
|
+
raw.title.length > 1000
|
|
13
|
+
)
|
|
14
|
+
throw Error("Invalid Linear issue metadata");
|
|
15
|
+
const url = issueReference(raw.url);
|
|
16
|
+
if (url.key !== identifier)
|
|
17
|
+
throw Error("Linear issue URL does not match its identifier");
|
|
18
|
+
const state = raw.state || { name: raw.status, type: raw.statusType };
|
|
19
|
+
if (
|
|
20
|
+
typeof state?.name !== "string" ||
|
|
21
|
+
typeof state?.type !== "string" ||
|
|
22
|
+
!Number.isFinite(Date.parse(raw.updatedAt)) ||
|
|
23
|
+
!Number.isFinite(Date.parse(checkedAt))
|
|
24
|
+
)
|
|
25
|
+
throw Error("Issue status or timestamps unavailable");
|
|
26
|
+
return {
|
|
27
|
+
id,
|
|
28
|
+
identifier,
|
|
29
|
+
title: raw.title,
|
|
30
|
+
url: url.url!,
|
|
31
|
+
state: { name: state.name.slice(0, 100), type: state.type.slice(0, 40) },
|
|
32
|
+
updatedAt: raw.updatedAt,
|
|
33
|
+
checkedAt,
|
|
34
|
+
...(raw.archivedAt ? { archivedAt: raw.archivedAt } : {}),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export class LinearIssuesReader {
|
|
38
|
+
constructor(
|
|
39
|
+
private token: string,
|
|
40
|
+
private workspaceId?: string,
|
|
41
|
+
private fetcher: typeof fetch = fetch,
|
|
42
|
+
) {}
|
|
43
|
+
/** Explicit, bounded title search. Never used to assign ownership automatically. */
|
|
44
|
+
async search(
|
|
45
|
+
query: string,
|
|
46
|
+
): Promise<{ issues: LinearWorkIssue[]; more: boolean }> {
|
|
47
|
+
const term = query.trim();
|
|
48
|
+
if (term.length < 3 || term.length > 120)
|
|
49
|
+
throw Error("Search with 3–120 characters");
|
|
50
|
+
const response = await this.fetcher("https://api.linear.app/graphql", {
|
|
51
|
+
method: "POST",
|
|
52
|
+
headers: {
|
|
53
|
+
"content-type": "application/json",
|
|
54
|
+
Authorization: this.token,
|
|
55
|
+
},
|
|
56
|
+
body: JSON.stringify({
|
|
57
|
+
query: `query FractalIssueSearch($term: String!) {
|
|
58
|
+
organization { id }
|
|
59
|
+
issues(first: 20, filter: { title: { containsIgnoreCase: $term } }) {
|
|
60
|
+
nodes { id identifier title url updatedAt archivedAt state { name type } }
|
|
61
|
+
pageInfo { hasNextPage }
|
|
62
|
+
}
|
|
63
|
+
}`,
|
|
64
|
+
variables: { term },
|
|
65
|
+
}),
|
|
66
|
+
signal: AbortSignal.timeout(10000),
|
|
67
|
+
});
|
|
68
|
+
if (response.status === 429)
|
|
69
|
+
throw Error("Linear rate limit reached; retry later");
|
|
70
|
+
if (!response.ok)
|
|
71
|
+
throw Error("Linear search failed (" + response.status + ")");
|
|
72
|
+
const body = await response.json();
|
|
73
|
+
if (
|
|
74
|
+
body.errors?.length ||
|
|
75
|
+
!Array.isArray(body.data?.issues?.nodes) ||
|
|
76
|
+
!body.data?.organization?.id
|
|
77
|
+
)
|
|
78
|
+
throw Error("Linear returned incomplete search results");
|
|
79
|
+
if (this.workspaceId && body.data.organization.id !== this.workspaceId)
|
|
80
|
+
throw Error("Linear credential belongs to a different workspace");
|
|
81
|
+
const checkedAt = new Date().toISOString();
|
|
82
|
+
return {
|
|
83
|
+
issues: body.data.issues.nodes.map((issue: unknown) =>
|
|
84
|
+
normalizeLinearIssue(issue, checkedAt),
|
|
85
|
+
),
|
|
86
|
+
more: !!body.data.issues.pageInfo?.hasNextPage,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
async issues(references: string[]): Promise<LinearWorkIssue[]> {
|
|
90
|
+
return (await this.issueResults(references)).issues;
|
|
91
|
+
}
|
|
92
|
+
/** Refresh every permitted registry link; unavailable issues are explicit, not old successes. */
|
|
93
|
+
async issueResults(
|
|
94
|
+
references: string[],
|
|
95
|
+
): Promise<{ issues: LinearWorkIssue[]; unavailable: string[] }> {
|
|
96
|
+
const keys = [...new Set(references.map((ref) => issueReference(ref).key))];
|
|
97
|
+
if (keys.length > 500)
|
|
98
|
+
throw Error("Refresh up to 500 linked issues at a time");
|
|
99
|
+
const result: LinearWorkIssue[] = [];
|
|
100
|
+
const unavailable: string[] = [];
|
|
101
|
+
for (let start = 0; start < keys.length; start += 25) {
|
|
102
|
+
const refs = keys.slice(start, start + 25),
|
|
103
|
+
definitions = refs.map((_, i) => `$id${i}: String!`).join(","),
|
|
104
|
+
queries = refs
|
|
105
|
+
.map(
|
|
106
|
+
(_, i) =>
|
|
107
|
+
`i${i}: issue(id:$id${i}) { id identifier title url updatedAt archivedAt state { name type } }`,
|
|
108
|
+
)
|
|
109
|
+
.join("\n");
|
|
110
|
+
const checkedAt = new Date().toISOString();
|
|
111
|
+
const response = await this.fetcher("https://api.linear.app/graphql", {
|
|
112
|
+
method: "POST",
|
|
113
|
+
headers: {
|
|
114
|
+
"content-type": "application/json",
|
|
115
|
+
Authorization: this.token,
|
|
116
|
+
},
|
|
117
|
+
body: JSON.stringify({
|
|
118
|
+
query: `query FractalIssues(${definitions}) { organization { id } ${queries} }`,
|
|
119
|
+
variables: Object.fromEntries(refs.map((ref, i) => ["id" + i, ref])),
|
|
120
|
+
}),
|
|
121
|
+
signal: AbortSignal.timeout(10000),
|
|
122
|
+
});
|
|
123
|
+
if (response.status === 429)
|
|
124
|
+
throw Error(
|
|
125
|
+
"Linear rate limit reached; recorded status remains available",
|
|
126
|
+
);
|
|
127
|
+
if (!response.ok)
|
|
128
|
+
throw Error("Linear status refresh failed (" + response.status + ")");
|
|
129
|
+
const body = await response.json();
|
|
130
|
+
if (
|
|
131
|
+
!body.data?.organization?.id ||
|
|
132
|
+
body.errors?.some(
|
|
133
|
+
(error: { path?: unknown[] }) =>
|
|
134
|
+
!/^i\d+$/.test(String(error.path?.[0] || "")),
|
|
135
|
+
)
|
|
136
|
+
)
|
|
137
|
+
throw Error(
|
|
138
|
+
"Linear returned incomplete issue data; recorded status remains available",
|
|
139
|
+
);
|
|
140
|
+
if (this.workspaceId && body.data.organization.id !== this.workspaceId)
|
|
141
|
+
throw Error("Linear credential belongs to a different workspace");
|
|
142
|
+
for (let i = 0; i < refs.length; i++)
|
|
143
|
+
if (
|
|
144
|
+
body.data["i" + i] &&
|
|
145
|
+
!body.errors?.some(
|
|
146
|
+
(error: { path?: unknown[] }) => error.path?.[0] === "i" + i,
|
|
147
|
+
)
|
|
148
|
+
)
|
|
149
|
+
result.push(normalizeLinearIssue(body.data["i" + i], checkedAt));
|
|
150
|
+
else unavailable.push(refs[i]);
|
|
151
|
+
}
|
|
152
|
+
return { issues: result, unavailable };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/** A desktop backend is a local workspace even when its UI is production-built.
|
|
2
|
+
* This is a capability flag, not authentication: every desktop request must also
|
|
3
|
+
* present the per-launch session and exact loopback host. */
|
|
4
|
+
export function desktopRuntime(
|
|
5
|
+
env: Record<string, string | undefined> = process.env,
|
|
6
|
+
) {
|
|
7
|
+
if (
|
|
8
|
+
env.FRACTORY_DESKTOP !== "1" ||
|
|
9
|
+
!/^[a-f0-9]{64}$/.test(env.FRACTORY_DESKTOP_TOKEN || "")
|
|
10
|
+
)
|
|
11
|
+
return false;
|
|
12
|
+
try {
|
|
13
|
+
const url = new URL(env.FRACTORY_DESKTOP_ORIGIN || "");
|
|
14
|
+
return (
|
|
15
|
+
url.protocol === "http:" &&
|
|
16
|
+
url.hostname === "127.0.0.1" &&
|
|
17
|
+
!!url.port &&
|
|
18
|
+
url.origin === env.FRACTORY_DESKTOP_ORIGIN
|
|
19
|
+
);
|
|
20
|
+
} catch {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export function localRepositoryRuntime(
|
|
25
|
+
env: Record<string, string | undefined> = process.env,
|
|
26
|
+
) {
|
|
27
|
+
return env.NODE_ENV !== "production" || desktopRuntime(env);
|
|
28
|
+
}
|
|
29
|
+
export function desktopRequestAllowed(
|
|
30
|
+
headers: Pick<Headers, "get">,
|
|
31
|
+
env: Record<string, string | undefined> = process.env,
|
|
32
|
+
) {
|
|
33
|
+
if (!desktopRuntime(env)) return false;
|
|
34
|
+
const expected = new URL(env.FRACTORY_DESKTOP_ORIGIN!);
|
|
35
|
+
if (headers.get("host") !== expected.host) return false;
|
|
36
|
+
const origin = headers.get("origin");
|
|
37
|
+
if (origin && origin !== expected.origin) return false;
|
|
38
|
+
const cookie = (headers.get("cookie") || "")
|
|
39
|
+
.split(";")
|
|
40
|
+
.map((part) => part.trim())
|
|
41
|
+
.find((part) => part.startsWith("fractory-session="))
|
|
42
|
+
?.slice("fractory-session=".length);
|
|
43
|
+
const token = env.FRACTORY_DESKTOP_TOKEN!;
|
|
44
|
+
if (!cookie || cookie.length !== token.length) return false;
|
|
45
|
+
let difference = 0;
|
|
46
|
+
for (let index = 0; index < token.length; index++)
|
|
47
|
+
difference |= token.charCodeAt(index) ^ cookie.charCodeAt(index);
|
|
48
|
+
return difference === 0;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Explicit loopback-only web development; packaged builds always require their session. */
|
|
52
|
+
export function developmentRequestAllowed(
|
|
53
|
+
headers: Pick<Headers, "get">,
|
|
54
|
+
env: Record<string, string | undefined> = process.env,
|
|
55
|
+
) {
|
|
56
|
+
if (env.NODE_ENV !== "development" || env.FRACTORY_WEB_DEV !== "1")
|
|
57
|
+
return false;
|
|
58
|
+
try {
|
|
59
|
+
const expected = new URL(env.FRACTORY_DEV_ORIGIN || "");
|
|
60
|
+
return (
|
|
61
|
+
expected.protocol === "http:" &&
|
|
62
|
+
expected.hostname === "127.0.0.1" &&
|
|
63
|
+
!!expected.port &&
|
|
64
|
+
headers.get("host") === expected.host &&
|
|
65
|
+
(!headers.get("origin") || headers.get("origin") === expected.origin)
|
|
66
|
+
);
|
|
67
|
+
} catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|