Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/extensions/replica-prototype/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
Expand Down Expand Up @@ -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 }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
Expand Down Expand Up @@ -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<typeof createHubOriginStore>,
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 }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 })
Expand Down
2 changes: 1 addition & 1 deletion .github/extensions/replica-prototype/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -570,10 +570,11 @@ export async function registerPrototypeCanvas(
const servers = new Map<string, Entry>()
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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"], {
Expand Down
Loading