Convex control plane (Slices 1-4 prerequisites for Slice 5): - Versioned Definition/Design persistence: revisions preserve history instead of deleting prior slices/approvals - Fenced conversation turn queue: attempt-numbered leases prevent stale worker overwrites; expired turns reconciled by cron - Fenced Run/Attempt claiming: only queued attempts from running Runs can be claimed; lease expiry checks on every finish/checkpoint - Multi-slice progression: successful slice marks next slice ready instead of completing the whole Work; completed Runs blocked from retry - Typed AttemptClassification in schema and resolver decisions table - Durable resolver decisions for normal outcomes, cancellation, and lease-expiry recovery - Persistent artifacts and delivery metadata with provenance, source revision, verification status, and controlled delivery transitions - High-impact open questions block definition approval - Question submission gated to defining/awaiting-approval states only - Delivery status updates validate JSON metadata before persisting - Planner failures recorded as durable blocked events - Approval identity uses canonical tokenIdentifier Primitives: - decodeDefinition separates decode from validate - Design validation enforces 1-4 slices with unique IDs and valid deps - Artifact and delivery draft schemas with verified-revision binding - Delivery status transition table Package management: - Consolidated shared deps into single catalog (hono, valibot, streamdown, @types/react, @types/node, @tailwindcss/*, react-native) - Removed cross-version Hono boundary: flue() exported as Fetchable - Deleted bun.lock and regenerated from clean catalog resolution Lint/format: - Removed .eslintignore; oxc uses native ignorePatterns in oxlint.config.ts - Deleted redundant packages/backend/.oxlintrc.json - Added repos/** and scripts/** to ignore patterns - Convex-specific rule overrides for ES2022 target constraints - Fixed all oxc errors in fluePersistence.ts and agents/src/db.ts - Fixed all formatting across changed files
179 lines
5.3 KiB
TypeScript
179 lines
5.3 KiB
TypeScript
import { CONTEXT_KINDS, contextPathForKind, MAX_CONTEXT_DOCUMENT_CHARACTERS } from '@code/primitives/project';
|
|
import type { PreparedPublicGitSource, PublicGitImportResult, RepositoryContextDocument } from '@code/primitives/project';
|
|
|
|
const GITHUB_HOST = "github.com";
|
|
const GITEA_HOST = "git.openputer.com";
|
|
const GITHUB_REQUEST_HEADERS = {
|
|
Accept: "application/vnd.github+json",
|
|
"User-Agent": "zopu-project-importer",
|
|
} as const;
|
|
const GITEA_REQUEST_HEADERS = {
|
|
Accept: "application/json",
|
|
"User-Agent": "zopu-project-importer",
|
|
} as const;
|
|
|
|
type Fetch = (
|
|
input: string | URL | Request,
|
|
init?: RequestInit
|
|
) => Promise<Response>;
|
|
|
|
interface PublicGitProvider {
|
|
readonly headers: Readonly<Record<string, string>>;
|
|
readonly metadataUrl: (source: PreparedPublicGitSource) => string;
|
|
readonly rawFileUrl: (
|
|
source: PreparedPublicGitSource,
|
|
branch: string,
|
|
path: string
|
|
) => string;
|
|
readonly repositoryNotFoundMessage: string;
|
|
}
|
|
|
|
const candidatePaths = {
|
|
agents: ["AGENTS.md"],
|
|
business: ["business.md", "docs/business.md"],
|
|
design: ["design.md", "docs/design.md"],
|
|
product: ["product.md", "docs/product.md"],
|
|
readme: ["README.md", "README", "readme.md"],
|
|
tech: ["tech.md", "docs/tech.md"],
|
|
} as const;
|
|
|
|
const encodePath = (path: string) =>
|
|
path
|
|
.split("/")
|
|
.map((segment) => encodeURIComponent(segment))
|
|
.join("/");
|
|
|
|
const providerForSource = (
|
|
source: PreparedPublicGitSource
|
|
): PublicGitProvider => {
|
|
if (source.host === GITHUB_HOST) {
|
|
return {
|
|
headers: GITHUB_REQUEST_HEADERS,
|
|
metadataUrl: (candidate) =>
|
|
`https://api.github.com/repos/${encodePath(candidate.repositoryPath)}`,
|
|
rawFileUrl: (candidate, branch, path) =>
|
|
`https://raw.githubusercontent.com/${encodePath(candidate.repositoryPath)}/${encodePath(branch)}/${encodePath(path)}`,
|
|
repositoryNotFoundMessage: "Public GitHub repository not found",
|
|
};
|
|
}
|
|
if (source.host === GITEA_HOST) {
|
|
return {
|
|
headers: GITEA_REQUEST_HEADERS,
|
|
metadataUrl: (candidate) =>
|
|
`https://${GITEA_HOST}/api/v1/repos/${encodePath(candidate.repositoryPath)}`,
|
|
rawFileUrl: (candidate, branch, path) =>
|
|
`https://${GITEA_HOST}/api/v1/repos/${encodePath(candidate.repositoryPath)}/raw/${encodePath(path)}?ref=${encodeURIComponent(branch)}`,
|
|
repositoryNotFoundMessage: "Public Gitea repository not found",
|
|
};
|
|
}
|
|
throw new Error(
|
|
"Repository import supports public GitHub and git.openputer.com URLs"
|
|
);
|
|
};
|
|
|
|
const fetchTextCandidate = async (
|
|
fetcher: Fetch,
|
|
provider: PublicGitProvider,
|
|
source: PreparedPublicGitSource,
|
|
branch: string,
|
|
path: string
|
|
): Promise<string | null> => {
|
|
const response = await fetcher(provider.rawFileUrl(source, branch, path), {
|
|
headers: provider.headers,
|
|
});
|
|
if (response.status === 404) {
|
|
return null;
|
|
}
|
|
if (!response.ok) {
|
|
throw new Error(
|
|
`${source.host} returned ${response.status} while reading ${path}`
|
|
);
|
|
}
|
|
const content = await response.text();
|
|
if (content.trim().length === 0) {
|
|
return null;
|
|
}
|
|
if (content.length > MAX_CONTEXT_DOCUMENT_CHARACTERS) {
|
|
throw new Error(`${path} exceeds the 200000 character import limit`);
|
|
}
|
|
return content;
|
|
};
|
|
|
|
const fetchContextDocument = async (
|
|
fetcher: Fetch,
|
|
provider: PublicGitProvider,
|
|
source: PreparedPublicGitSource,
|
|
branch: string,
|
|
kind: (typeof CONTEXT_KINDS)[number]
|
|
): Promise<RepositoryContextDocument | null> => {
|
|
for (const candidate of candidatePaths[kind]) {
|
|
const content = await fetchTextCandidate(
|
|
fetcher,
|
|
provider,
|
|
source,
|
|
branch,
|
|
candidate
|
|
);
|
|
if (content) {
|
|
return {
|
|
content,
|
|
kind,
|
|
path: contextPathForKind(kind),
|
|
};
|
|
}
|
|
}
|
|
return null;
|
|
};
|
|
|
|
const readDefaultBranch = async (
|
|
fetcher: Fetch,
|
|
provider: PublicGitProvider,
|
|
source: PreparedPublicGitSource
|
|
): Promise<string> => {
|
|
const response = await fetcher(provider.metadataUrl(source), {
|
|
headers: provider.headers,
|
|
});
|
|
if (!response.ok) {
|
|
throw new Error(
|
|
response.status === 404
|
|
? provider.repositoryNotFoundMessage
|
|
: `${source.host} returned ${response.status} while reading the repository`
|
|
);
|
|
}
|
|
const metadata = (await response.json()) as { default_branch?: unknown };
|
|
if (
|
|
typeof metadata.default_branch !== "string" ||
|
|
metadata.default_branch.length === 0
|
|
) {
|
|
throw new Error(`${source.host} did not return a default branch`);
|
|
}
|
|
return metadata.default_branch;
|
|
};
|
|
|
|
export const inspectPublicGit = async (
|
|
source: PreparedPublicGitSource,
|
|
options: { readonly fetch?: Fetch } = {}
|
|
): Promise<PublicGitImportResult> => {
|
|
const fetcher = options.fetch ?? globalThis.fetch;
|
|
const provider = providerForSource(source);
|
|
const defaultBranch = await readDefaultBranch(fetcher, provider, source);
|
|
const candidates = await Promise.all(
|
|
CONTEXT_KINDS.map((kind) =>
|
|
fetchContextDocument(fetcher, provider, source, defaultBranch, kind)
|
|
)
|
|
);
|
|
const documents = candidates.filter(
|
|
(document): document is RepositoryContextDocument => document !== null
|
|
);
|
|
return {
|
|
defaultBranch,
|
|
documents,
|
|
warnings: CONTEXT_KINDS.filter(
|
|
(kind) => !documents.some((document) => document.kind === kind)
|
|
).map((kind) => ({
|
|
message: "Canonical context file was not found in the repository",
|
|
path: contextPathForKind(kind),
|
|
})),
|
|
};
|
|
};
|