-
Notifications
You must be signed in to change notification settings - Fork 33
feat(cloud-agent): add git_signed_rewrite tool #2486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 3 additions & 38 deletions
41
packages/agent/src/adapters/local-tools/tools/signed-commit.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,14 @@ | ||
| import * as path from "node:path"; | ||
| import { isCloudRun } from "../../../utils/common"; | ||
| import { resolveGithubToken } from "../../../utils/github-token"; | ||
| import { | ||
| runSignedCommitTool, | ||
| SIGNED_COMMIT_TOOL_DESCRIPTION, | ||
| SIGNED_COMMIT_TOOL_NAME, | ||
| signedCommitToolSchema, | ||
| } from "../../signed-commit-shared"; | ||
| import { defineLocalTool } from "../registry"; | ||
| import { defineSignedGitTool } from "./signed-git-tool"; | ||
|
|
||
| /** | ||
| * `git_signed_commit` as a local tool. Cloud-run only; the token is resolved | ||
| * lazily at call time so the tool stays visible even when the GitHub token | ||
| * lands in `process.env` after the session was created (e.g. an orchestrator | ||
| * injecting it post-spawn). Committing is core to cloud tasks, so keep it | ||
| * exposed past ToolSearch via `alwaysLoad`. | ||
| */ | ||
| export const signedCommitTool = defineLocalTool({ | ||
| export const signedCommitTool = defineSignedGitTool({ | ||
| name: SIGNED_COMMIT_TOOL_NAME, | ||
| description: SIGNED_COMMIT_TOOL_DESCRIPTION, | ||
| schema: signedCommitToolSchema, | ||
| alwaysLoad: true, | ||
| isEnabled: (_ctx, meta) => isCloudRun(meta), | ||
| handler: (ctx, args) => { | ||
| // Prefer a freshly-resolved token (reads the live agentsh env file) over | ||
| // the one captured at session setup, so a mid-session credential refresh | ||
| // takes effect without rebuilding the session. | ||
| const token = resolveGithubToken() ?? ctx.token; | ||
| if (!token) { | ||
| return Promise.resolve({ | ||
| content: [ | ||
| { | ||
| type: "text" as const, | ||
| text: `${SIGNED_COMMIT_TOOL_NAME} failed: no GitHub token in env (GH_TOKEN/GITHUB_TOKEN)`, | ||
| }, | ||
| ], | ||
| isError: true, | ||
| }); | ||
| } | ||
| // Resolve an explicit `cwd` arg against the session cwd so the agent can | ||
| // commit from any clone reachable in the sandbox, not just the one the | ||
| // session was rooted at. Absolute paths fall through `path.resolve` | ||
| // unchanged; relative paths join the session cwd. | ||
| const { cwd: argCwd, ...input } = args; | ||
| const cwd = argCwd ? path.resolve(ctx.cwd, argCwd) : ctx.cwd; | ||
| return runSignedCommitTool({ cwd, token, taskId: ctx.taskId }, input); | ||
| }, | ||
| run: runSignedCommitTool, | ||
| }); |
48 changes: 48 additions & 0 deletions
48
packages/agent/src/adapters/local-tools/tools/signed-git-tool.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import * as path from "node:path"; | ||
| import type { SignedCommitCtx } from "@posthog/git/signed-commit"; | ||
| import type { z } from "zod"; | ||
| import { isCloudRun } from "../../../utils/common"; | ||
| import { resolveGithubToken } from "../../../utils/github-token"; | ||
| import type { SignedCommitToolResult } from "../../signed-commit-shared"; | ||
| import { defineLocalTool, type LocalTool } from "../registry"; | ||
|
|
||
| /** | ||
| * Factory for the cloud-only signed-git tools (git_signed_commit / git_signed_rewrite). | ||
| * Resolves the token lazily (live /tmp/agent-env first, so a mid-session credential | ||
| * refresh takes effect) and the optional `cwd` arg against the session cwd, then | ||
| * delegates to the tool's `run`. Kept past ToolSearch via alwaysLoad. | ||
| */ | ||
| export function defineSignedGitTool<S extends z.ZodRawShape, R>(opts: { | ||
| name: string; | ||
| description: string; | ||
| schema: S; | ||
| run: (ctx: SignedCommitCtx, input: R) => Promise<SignedCommitToolResult>; | ||
| }): LocalTool { | ||
| return defineLocalTool({ | ||
| name: opts.name, | ||
| description: opts.description, | ||
| schema: opts.schema, | ||
| alwaysLoad: true, | ||
| isEnabled: (_ctx, meta) => isCloudRun(meta), | ||
| handler: (ctx, args) => { | ||
| const token = resolveGithubToken() ?? ctx.token; | ||
| if (!token) { | ||
| return Promise.resolve({ | ||
| content: [ | ||
| { | ||
| type: "text" as const, | ||
| text: `${opts.name} failed: no GitHub token in env (GH_TOKEN/GITHUB_TOKEN)`, | ||
| }, | ||
| ], | ||
| isError: true as const, | ||
| }); | ||
| } | ||
| const { cwd: argCwd, ...input } = args as { cwd?: string } & Record< | ||
| string, | ||
| unknown | ||
| >; | ||
| const cwd = argCwd ? path.resolve(ctx.cwd, argCwd) : ctx.cwd; | ||
| return opts.run({ cwd, token, taskId: ctx.taskId }, input as R); | ||
| }, | ||
| }); | ||
| } |
14 changes: 14 additions & 0 deletions
14
packages/agent/src/adapters/local-tools/tools/signed-rewrite.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { | ||
| runSignedRewriteTool, | ||
| SIGNED_REWRITE_TOOL_DESCRIPTION, | ||
| SIGNED_REWRITE_TOOL_NAME, | ||
| signedRewriteToolSchema, | ||
| } from "../../signed-commit-shared"; | ||
| import { defineSignedGitTool } from "./signed-git-tool"; | ||
|
|
||
| export const signedRewriteTool = defineSignedGitTool({ | ||
| name: SIGNED_REWRITE_TOOL_NAME, | ||
| description: SIGNED_REWRITE_TOOL_DESCRIPTION, | ||
| schema: signedRewriteToolSchema, | ||
| run: runSignedRewriteTool, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.