diff --git a/.github/extensions/replica-prototype/README.md b/.github/extensions/replica-prototype/README.md index 358cada0b..0f6b068af 100644 --- a/.github/extensions/replica-prototype/README.md +++ b/.github/extensions/replica-prototype/README.md @@ -78,6 +78,8 @@ New improvements remain local until their captured source matches a successful p When this repository has a Hub connection and a Hub administrator connected Copilot, each prompt you submit gets the [learnings](../../../hub/docs/learnings.md) your repository's administrators accepted in Hub. The extension never sends your prompt to Hub. Copilot treats the learnings as guidance from your team, not as commands. Its first successful answer replaces earlier learning context, including after a restart or session resume; an empty answer explicitly clears it. Later unchanged answers add nothing. When learnings change, or Learnings is turned off, the next successful answer replaces the earlier guidance. It adds nothing when Hub's answer is not in the form that `replica-learning` writes. It never delays a prompt by more than three seconds and never fails it. Without a Hub connection it adds nothing and never asks you to connect. If you connected before the approval page mentioned learnings, disconnect and connect again to get them. +The extension registers the prompt hook only when the startup repository already has a saved Hub selection or a configured Hub URL. Connecting a repository during a session enables learnings in the next session. + Copilot CLI runs extensions only while its extensions feature is on. In Copilot CLI 1.0.96 that is an experimental feature: start Copilot CLI with `--experimental`, or run `/experimental on`. In prompt mode (`copilot -p`), it loads a repository's project copy only when `GITHUB_COPILOT_PROMPT_MODE_EXTENSIONS=true`. ## Agent access to Hub comments diff --git a/.github/extensions/replica-prototype/create-learnings-hook/index.mjs b/.github/extensions/replica-prototype/create-learnings-hook/index.mjs index 93876a0f0..7e10bc1a1 100644 --- a/.github/extensions/replica-prototype/create-learnings-hook/index.mjs +++ b/.github/extensions/replica-prototype/create-learnings-hook/index.mjs @@ -112,20 +112,7 @@ export function createLearningsHook(options = {}) { async function recall(input) { const repositoryPath = readHubObject(input)?.workingDirectory if (typeof repositoryPath !== "string" || !repositoryPath) return null - // A local Hub names a checkout under .local/prototypes/ by its folder; elsewhere this is ignored. - const context = { - repositoryPath, - target: { repository: `local/${path.basename(repositoryPath)}` }, - } - const { repository, legacyRepository } = await readCanvasRepository(context) - if (!repository) return null - const origin = - (await origins.read(repository)) || - (legacyRepository && legacyRepository !== repository - ? await origins.read(legacyRepository) - : null) || - options.origin || - process.env.REPLICA_HUB_ORIGIN + const { context, origin } = await resolveHubConnection(repositoryPath, origins, options) if (!origin) return null return createHubClient(context, { ...options, origin }).recallLearnings({ timeoutMs }) } @@ -165,3 +152,34 @@ export function createLearningsHook(options = {}) { } } } +/** Whether this checkout has a Hub origin configured for prompt learnings. */ +export async function hasLearningsHubOrigin(repositoryPath, options = {}) { + try { + const { origin } = await resolveHubConnection( + repositoryPath, + createHubOriginStore(options), + options, + ) + return Boolean(origin) + } catch (error) { + console.error("Replica learnings are disabled because the Hub origin could not be read.", error) + return false + } +} +async function resolveHubConnection(repositoryPath, origins, options) { + // A local Hub names a checkout under .local/prototypes/ by its folder; elsewhere this is ignored. + const context = { + repositoryPath, + target: { repository: `local/${path.basename(repositoryPath)}` }, + } + const { repository, legacyRepository } = await readCanvasRepository(context) + if (!repository) return { context, origin: null } + const origin = + (await origins.read(repository)) || + (legacyRepository && legacyRepository !== repository + ? await origins.read(legacyRepository) + : null) || + options.origin || + process.env.REPLICA_HUB_ORIGIN + return { context, origin } +} diff --git a/.github/extensions/replica-prototype/create-learnings-hook/index.mts b/.github/extensions/replica-prototype/create-learnings-hook/index.mts index a48f5750b..5a776ab54 100644 --- a/.github/extensions/replica-prototype/create-learnings-hook/index.mts +++ b/.github/extensions/replica-prototype/create-learnings-hook/index.mts @@ -122,20 +122,7 @@ export function createLearningsHook(options: LearningsHookOptions = {}) { async function recall(input: unknown) { const repositoryPath = readHubObject(input)?.workingDirectory if (typeof repositoryPath !== "string" || !repositoryPath) return null - // A local Hub names a checkout under .local/prototypes/ by its folder; elsewhere this is ignored. - const context = { - repositoryPath, - target: { repository: `local/${path.basename(repositoryPath)}` }, - } - const { repository, legacyRepository } = await readCanvasRepository(context) - if (!repository) return null - const origin = - (await origins.read(repository)) || - (legacyRepository && legacyRepository !== repository - ? await origins.read(legacyRepository) - : null) || - options.origin || - process.env.REPLICA_HUB_ORIGIN + const { context, origin } = await resolveHubConnection(repositoryPath, origins, options) if (!origin) return null return createHubClient(context, { ...options, origin }).recallLearnings({ timeoutMs }) } @@ -176,3 +163,43 @@ export function createLearningsHook(options: LearningsHookOptions = {}) { } } } + +/** Whether this checkout has a Hub origin configured for prompt learnings. */ +export async function hasLearningsHubOrigin( + repositoryPath: string, + options: LearningsHookOptions = {}, +) { + try { + const { origin } = await resolveHubConnection( + repositoryPath, + createHubOriginStore(options), + options, + ) + return Boolean(origin) + } catch (error) { + console.error("Replica learnings are disabled because the Hub origin could not be read.", error) + return false + } +} + +async function resolveHubConnection( + repositoryPath: string, + origins: ReturnType, + options: LearningsHookOptions, +) { + // A local Hub names a checkout under .local/prototypes/ by its folder; elsewhere this is ignored. + const context = { + repositoryPath, + target: { repository: `local/${path.basename(repositoryPath)}` }, + } + const { repository, legacyRepository } = await readCanvasRepository(context) + if (!repository) return { context, origin: null } + const origin = + (await origins.read(repository)) || + (legacyRepository && legacyRepository !== repository + ? await origins.read(legacyRepository) + : null) || + options.origin || + process.env.REPLICA_HUB_ORIGIN + return { context, origin } +} diff --git a/.github/extensions/replica-prototype/create-learnings-hook/index.test.mjs b/.github/extensions/replica-prototype/create-learnings-hook/index.test.mjs index 01ed6b3d1..af5168f67 100644 --- a/.github/extensions/replica-prototype/create-learnings-hook/index.test.mjs +++ b/.github/extensions/replica-prototype/create-learnings-hook/index.test.mjs @@ -6,7 +6,8 @@ import path from "node:path" import { afterEach, beforeEach, describe, it } from "node:test" import { rankForRecall, renderRecall } from "../../../../packages/learning/src/recall.mjs" -import { createLearningsHook } from "./index.mjs" +import { createHubOriginStore } from "../create-hub-origin-store/index.mjs" +import { createLearningsHook, hasLearningsHubOrigin } from "./index.mjs" const RENEWAL = "/api/canvas/connection/renewal" @@ -492,6 +493,38 @@ esac }) }) + it("only enables prompt learnings for a repository with a configured Hub origin", async () => { + const originalOrigin = process.env.REPLICA_HUB_ORIGIN + delete process.env.REPLICA_HUB_ORIGIN + try { + assert.equal(await hasLearningsHubOrigin(workspace, { directory }), false) + assert.equal(await hasLearningsHubOrigin(workspace, { directory, origin }), true) + + await createHubOriginStore({ directory }).save("github/replica", origin) + assert.equal(await hasLearningsHubOrigin(workspace, { directory }), true) + } finally { + if (originalOrigin !== undefined) process.env.REPLICA_HUB_ORIGIN = originalOrigin + } + }) + + it("disables prompt learnings and reports an invalid saved Hub origin", async () => { + await createHubOriginStore({ directory }).save("github/replica", origin) + const key = createHash("sha256").update("github/replica").digest("hex") + await writeFile(path.join(directory, `${key}.origin.json`), "{}") + const errors = [] + const originalError = console.error + console.error = (...values) => errors.push(values) + + try { + assert.equal(await hasLearningsHubOrigin(workspace, { directory }), false) + } finally { + console.error = originalError + } + + assert.equal(errors.length, 1) + assert.match(errors[0][0], /Replica learnings are disabled/) + }) + it("withdraws earlier guidance after its session history was evicted", async () => { await connect() const hook = createLearningsHook({ origin, directory }) diff --git a/.github/extensions/replica-prototype/plugin.json b/.github/extensions/replica-prototype/plugin.json index 92a3fa8ca..92e4318db 100644 --- a/.github/extensions/replica-prototype/plugin.json +++ b/.github/extensions/replica-prototype/plugin.json @@ -2,7 +2,7 @@ "$schema": "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json", "name": "replica-prototype", "description": "Wrap a running prototype with Replica publish controls and repository status.", - "version": "1.5.6", + "version": "1.5.7", "author": { "name": "GitHub" }, diff --git a/.github/extensions/replica-prototype/register-prototype-canvas/index.mjs b/.github/extensions/replica-prototype/register-prototype-canvas/index.mjs index a1734b8f5..0d03943e3 100644 --- a/.github/extensions/replica-prototype/register-prototype-canvas/index.mjs +++ b/.github/extensions/replica-prototype/register-prototype-canvas/index.mjs @@ -24,7 +24,7 @@ import { reviewAction } from "../review-action/index.mjs" import { projectValidation, validateControls } from "../validate-controls/index.mjs" import { projectControlsStatus } from "../controls-status/index.mjs" import { improvementBatchResult } from "../improvement-batch-result/index.mjs" -import { createLearningsHook } from "../create-learnings-hook/index.mjs" +import { createLearningsHook, hasLearningsHubOrigin } from "../create-learnings-hook/index.mjs" import { gitShaPattern as COMMIT_PATTERN } from "../identity-patterns/index.mjs" export { improvementBatchResult } from "../improvement-batch-result/index.mjs" function retainRepositoryState(next, previous) { @@ -451,10 +451,11 @@ export async function registerPrototypeCanvas( sdk || (await import("@github/copilot-sdk/extension")) const servers = new Map() const devServers = createDevServerManager({ discover }) + const hooks = (await hasLearningsHubOrigin(process.cwd(), canvasOptions.hubOptions)) + ? { onUserPromptSubmitted: createLearningsHook(canvasOptions.hubOptions) } + : undefined const session = await joinSession({ - hooks: { - onUserPromptSubmitted: createLearningsHook(canvasOptions.hubOptions), - }, + ...(hooks ? { hooks } : {}), canvases: [ createCanvas({ id: canvasOptions.id ?? "replica-prototype", diff --git a/.github/extensions/replica-prototype/register-prototype-canvas/index.mts b/.github/extensions/replica-prototype/register-prototype-canvas/index.mts index 3f380b56c..07f8cb9cc 100644 --- a/.github/extensions/replica-prototype/register-prototype-canvas/index.mts +++ b/.github/extensions/replica-prototype/register-prototype-canvas/index.mts @@ -24,7 +24,7 @@ import { reviewAction } from "../review-action/index.mjs" import { projectValidation, validateControls } from "../validate-controls/index.mjs" import { projectControlsStatus } from "../controls-status/index.mjs" import { improvementBatchResult } from "../improvement-batch-result/index.mjs" -import { createLearningsHook } from "../create-learnings-hook/index.mjs" +import { createLearningsHook, hasLearningsHubOrigin } from "../create-learnings-hook/index.mjs" import { gitShaPattern as COMMIT_PATTERN } from "../identity-patterns/index.mjs" import type { CanvasContext } from "../create-canvas-context/index.mjs" import type { CanvasServer } from "../start-canvas-server/index.mjs" @@ -570,10 +570,11 @@ export async function registerPrototypeCanvas( const servers = new Map() const devServers = createDevServerManager({ discover }) + const hooks = (await hasLearningsHubOrigin(process.cwd(), canvasOptions.hubOptions)) + ? { onUserPromptSubmitted: createLearningsHook(canvasOptions.hubOptions) } + : undefined const session = await joinSession({ - hooks: { - onUserPromptSubmitted: createLearningsHook(canvasOptions.hubOptions), - }, + ...(hooks ? { hooks } : {}), canvases: [ createCanvas({ id: canvasOptions.id ?? "replica-prototype", diff --git a/.github/extensions/replica-prototype/register-prototype-canvas/index.test.mjs b/.github/extensions/replica-prototype/register-prototype-canvas/index.test.mjs index f45b17ece..7e351ad0b 100644 --- a/.github/extensions/replica-prototype/register-prototype-canvas/index.test.mjs +++ b/.github/extensions/replica-prototype/register-prototype-canvas/index.test.mjs @@ -132,6 +132,31 @@ async function register( } describe("registerPrototypeCanvas()", () => { + it("does not register prompt learnings without a saved or configured Hub URL", async () => { + await withContext(async (context) => { + const originalOrigin = process.env.REPLICA_HUB_ORIGIN + delete process.env.REPLICA_HUB_ORIGIN + try { + let registered + const directory = path.join(context.sessionWorkspacePath, "hub-credentials") + const canvas = await register( + context, + [], + undefined, + { hubOptions: { directory } }, + (configuration) => { + registered = configuration + }, + ) + + assert.equal(registered.hooks, undefined) + assert.equal(registered.canvases[0], canvas) + } finally { + if (originalOrigin !== undefined) process.env.REPLICA_HUB_ORIGIN = originalOrigin + } + }) + }) + it("uses a saved Hub connection for creation without a local preview or new approval", async () => { await withContext(async (context) => { execFileSync("git", ["remote", "add", "origin", "https://github.com/github/replica.git"], {