Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(context-layer): install git in the runtime image (#89276)

posthog[bot]70965148cc5c
diff --git a/Dockerfile b/Dockerfile
index 4eb1c51c488c..318f6e599984 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -330,6 +330,7 @@ RUN apt-get update && \
RUN apt-get update && \
apt-get install -y --no-install-recommends --allow-downgrades \
"gettext-base" \
+ "git" \
"libpq5" \
"libxmlsec1=1.2.37-2" \
"libxmlsec1-openssl=1.2.37-2" \
diff --git a/products/context_layer/backend/facade/api.py b/products/context_layer/backend/facade/api.py
index 20462c682f26..6a188ca0ba84 100644
--- a/products/context_layer/backend/facade/api.py
+++ b/products/context_layer/backend/facade/api.py
@@ -40,6 +40,7 @@ from products.context_layer.backend.store import (
BundleConflictError,
CommitAuthor,
ContextLayerStoreError,
+ DependencyUnavailableError,
HeadConflictError,
LintFailedError,
RepoLockUnavailableError,
@@ -71,6 +72,7 @@ __all__ = [
"CommitAuthor",
"ContextLayerMount",
"ContextLayerStoreError",
+ "DependencyUnavailableError",
"HeadConflictError",
"InvalidPagePathError",
"LintFailedError",
diff --git a/products/context_layer/backend/presentation/views.py b/products/context_layer/backend/presentation/views.py
index 6e951852cbb1..8a2c038c638c 100644
--- a/products/context_layer/backend/presentation/views.py
+++ b/products/context_layer/backend/presentation/views.py
@@ -59,6 +59,11 @@ def _store_error_response(error: facade.ContextLayerStoreError) -> Response:
{"detail": "The change violates the wiki structure.", "errors": error.errors},
status=status.HTTP_400_BAD_REQUEST,
)
+ if isinstance(error, facade.DependencyUnavailableError):
+ return Response(
+ {"detail": "The context layer is temporarily unavailable. Try again later."},
+ status=status.HTTP_503_SERVICE_UNAVAILABLE,
+ )
raise error
diff --git a/products/context_layer/backend/store.py b/products/context_layer/backend/store.py
index f19786d6e6a4..2825a8eb57e8 100644
--- a/products/context_layer/backend/store.py
+++ b/products/context_layer/backend/store.py
@@ -60,6 +60,12 @@ class ContextLayerStoreError(Exception):
pass
+class DependencyUnavailableError(ContextLayerStoreError):
+ """A binary the store shells out to (git) is missing from the host, so no
+ read or write can run. Maps to HTTP 503 so the API fails cleanly instead of
+ an unhandled 500."""
+
+
class RepoNotFoundError(ContextLayerStoreError):
"""The organization has no context layer repo (no config row or no bundle)."""
@@ -194,23 +200,26 @@ def repo_writer_lock(organization_id: uuid.UUID | str) -> Iterator[None]:
def _run_git(args: list[str], cwd: Path, stdin_text: str | None = None) -> str:
- result = subprocess.run(
- [
- "git",
- "-c",
- f"user.name={COMMITTER_NAME}",
- "-c",
- f"user.email={COMMITTER_EMAIL}",
- "-c",
- "commit.gpgsign=false",
- *args,
- ],
- cwd=cwd,
- input=stdin_text,
- capture_output=True,
- text=True,
- timeout=GIT_TIMEOUT_SECONDS,
- )
+ try:
+ result = subprocess.run(
+ [
+ "git",
+ "-c",
+ f"user.name={COMMITTER_NAME}",
+ "-c",
+ f"user.email={COMMITTER_EMAIL}",
+ "-c",
+ "commit.gpgsign=false",
+ *args,
+ ],
+ cwd=cwd,
+ input=stdin_text,
+ capture_output=True,
+ text=True,
+ timeout=GIT_TIMEOUT_SECONDS,
+ )
+ except FileNotFoundError as err:
+ raise DependencyUnavailableError("git binary is not available") from err
if result.returncode != 0:
raise ContextLayerStoreError(f"git {args[0]} failed: {result.stderr.strip()}")
return result.stdout.strip()
diff --git a/products/context_layer/backend/test/test_api.py b/products/context_layer/backend/test/test_api.py
index 548dabf9d0a8..7f9a08ca669a 100644
--- a/products/context_layer/backend/test/test_api.py
+++ b/products/context_layer/backend/test/test_api.py
@@ -239,6 +239,15 @@ class TestContextLayerAPI(APIBaseTest):
response = self.client.post(f"{self.base_url}/enable/")
assert response.status_code == 429
+ def test_enable_returns_503_when_git_binary_is_missing(self, _flag) -> None:
+ # A host without git makes every store write shell out to a missing
+ # binary. The endpoint must map that to a clean 503, not an unhandled 500.
+ with patch.object(
+ store.subprocess, "run", side_effect=FileNotFoundError(2, "No such file or directory", "git")
+ ):
+ response = self.client.post(f"{self.base_url}/enable/")
+ assert response.status_code == 503, response.content
+
def test_endpoints_404_before_enablement(self, _flag) -> None:
assert self.client.get(f"{self.base_url}/tree/").status_code == 404
assert self.client.get(f"{self.base_url}/status/").status_code == 404
diff --git a/products/context_layer/backend/test/test_store.py b/products/context_layer/backend/test/test_store.py
index 02ff8467991b..d8738a56c61b 100644
--- a/products/context_layer/backend/test/test_store.py
+++ b/products/context_layer/backend/test/test_store.py
@@ -31,6 +31,15 @@ class TestRepoWriterLock(SimpleTestCase):
assert client.eval.called
+class TestRunGit(SimpleTestCase):
+ def test_missing_git_binary_raises_store_error(self) -> None:
+ with patch.object(
+ store.subprocess, "run", side_effect=FileNotFoundError(2, "No such file or directory", "git")
+ ):
+ with self.assertRaises(store.DependencyUnavailableError):
+ store._run_git(["status"], cwd=Path("/tmp"))
+
+
class TestDreamPathGuard(SimpleTestCase):
def test_allows_context_pages_and_rejects_server_owned_paths(self) -> None:
assert store._dream_may_edit("org/strategy.md")