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/workflows/fork-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,5 @@ jobs:
- name: MCP search scenarios
run: |
bun run e2e:local --test-name 'MCP search resolves namespace-relative dotted paths|A broad MCP tool search returns concise pages'
- name: MCP search kept-order scenarios
run: bun run e2e:self-host --test-name '^A search being paged keeps its order|^An MCP search without a query pages|^A held kept-order read' --workers 1
45 changes: 45 additions & 0 deletions apps/hosted/testing/monotonic-clock-fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Test host only: advances Effect's monotonic clock so a scenario can age a kept search order
* while a read waits. Wall time, sleeps, and deadlines based on them use the live clock.
* Production entry points never provide it.
*/
import { Clock, Effect, Layer, Schema } from "effect";
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";

const path = "/api/devtools/monotonic-clock";
const Advance = Schema.Struct({ advanceMillis: Schema.Int.check(Schema.isGreaterThan(0)) });

export const monotonicClockFixture = Effect.gen(function* () {
const live = yield* Clock.Clock;
let offset = 0n;
const nanos = () => live.monotonicTimeNanosUnsafe() + offset;
const clock = Layer.succeed(Clock.Clock, {
currentTimeMillisUnsafe: () => live.currentTimeMillisUnsafe(),
currentTimeMillis: live.currentTimeMillis,
currentTimeNanosUnsafe: () => live.currentTimeNanosUnsafe(),
currentTimeNanos: live.currentTimeNanos,
sleep: (duration) => live.sleep(duration),
monotonicTimeNanosUnsafe: nanos,
monotonicTimeNanos: Effect.sync(nanos),
});
const routes = Layer.mergeAll(
HttpRouter.add(
"POST",
path,
Effect.gen(function* () {
const { advanceMillis } = yield* HttpServerRequest.schemaBodyJson(Advance);
offset += BigInt(advanceMillis) * 1_000_000n;
return HttpServerResponse.jsonUnsafe({ advanced: true });
}).pipe(Effect.orDie),
),
HttpRouter.add(
"DELETE",
path,
Effect.sync(() => {
offset = 0n;
return HttpServerResponse.jsonUnsafe({ reset: true });
}),
),
);
return { clock, routes };
});
4 changes: 4 additions & 0 deletions apps/hosted/testing/self-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { devAppName, freePort } from "../../../scripts/dev-host.ts";
import { developmentSettings, developmentSignIn } from "./development.ts";
import { accessCheckFixture } from "./access-check-fixture.ts";
import { statementHoldFixture } from "./statement-hold-fixture.ts";
import { monotonicClockFixture } from "./monotonic-clock-fixture.ts";
import { storageFaultFixture } from "./storage-fault-fixture.ts";
import { apiDocumentFixture } from "./api-document-fixture.ts";

Expand Down Expand Up @@ -54,6 +55,7 @@ const command = Command.make("test-self-host", {
const target = yield* developmentSettings;
const directory = yield* Config.NonEmptyString("EXECUTOR_DATA_DIR");
const storageFault = yield* storageFaultFixture;
const monotonic = yield* monotonicClockFixture;
const server = Layer.unwrap(
Effect.gen(function* () {
const development = yield* developmentSignIn(target, organization);
Expand All @@ -63,6 +65,7 @@ const command = Command.make("test-self-host", {
HttpRouter.add("GET", "/api/devtools", development.status),
HttpRouter.add("POST", "/api/devtools/operator", development.signIn),
statementHold.routes,
monotonic.routes,
storageFault.routes,
yield* apiDocumentFixture,
product,
Expand All @@ -88,6 +91,7 @@ const command = Command.make("test-self-host", {
yield* Console.log(`Starting local test server at ${target.origin}/login`);
// Fibers inherit the reference, so every statement the server runs reaches the fault.
return yield* Layer.launch(server).pipe(
Effect.provide(monotonic.clock),
Effect.provideService(Statement.CurrentTransformer, storageFault.transformer),
);
}).pipe(Effect.provideServiceEffect(ConfigProvider.ConfigProvider, testServerConfiguration)),
Expand Down
37 changes: 27 additions & 10 deletions apps/hosted/testing/statement-hold-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* personal access token check an MCP tool call makes, until the scenario releases it, so a
* scenario can change saved state while that check waits. It can instead hold a request just after
* its check of the caller's access to one app or one account has answered, so a scenario can
* revoke that access between the check and the write the request makes next. It can instead fail
* the next read of account rows an MCP tool call makes, as a dropped connection would, so a
* scenario can see what the call does with a read that failed for storage. PostgreSQL reads take no
* row locks, so no lock on rows a scenario created can hold them; the test host holds the statement
* before it reaches the database, or after its reply, instead. Production entry points never
* provide it.
* revoke that access between the check and the write the request makes next. It can hold a kept
* search page after SQL answers so a scenario can change the order cache before the read resumes.
* It can instead fail the next read of account rows an MCP tool call makes, as a dropped
* connection would, to see how the call handles a storage failure. PostgreSQL reads take no row
* locks, so no lock on rows a scenario created can hold them; the test host holds the statement
* before it reaches the database, or after its reply, instead. Production entry points never provide it.
*/
import { Deferred, Effect, Layer, Option, Schema, type Tracer } from "effect";
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
Expand All @@ -34,6 +34,8 @@ const answeredChecks = {
"app-access": "left join hosted_app_access p on",
"account-access": "select p.account_id as account",
} as const;
/** The row lookup by ID in a kept-order page, not the ranking statement. */
const keptOrderPage = "where s.id in (select value from json_array_elements_text(";
const path = "/api/devtools/statement-hold";
const Arm = Schema.Union([
// `toolCall` holds only a check made inside an MCP tool call, not one made by discovery.
Expand All @@ -43,6 +45,8 @@ const Arm = Schema.Union([
after: Schema.Literals(["app-access", "account-access"]),
id: Schema.NonEmptyString,
}),
// Holds the next kept-order page after SQL answers; only tool search runs that statement.
Schema.Struct({ after: Schema.Literal("kept-order") }),
Schema.Struct({ authentication: Schema.Literal("tool-call") }),
// Fails the next read of account rows inside a tool call instead of holding it.
Schema.Struct({ fail: Schema.Literal("tool-call-accounts") }),
Expand All @@ -64,8 +68,11 @@ const inToolCall = Effect.map(Effect.currentParentSpan, (span) => {
interface Hold {
/** The app whose access check to hold, or the next PAT check inside a tool call. */
readonly app: string | undefined;
/** Hold the next answered check of this kind that binds this ID, instead. */
readonly after: { readonly check: keyof typeof answeredChecks; readonly id: string } | undefined;
/** Hold the next answered check binding this ID, or the next answered kept-order page. */
readonly after:
| { readonly check: keyof typeof answeredChecks; readonly id: string }
| { readonly check: "kept-order" }
| undefined;
/** Hold the app's check only inside a tool call. */
readonly toolCall: boolean;
/** Fail the next read of account rows inside a tool call; nothing is held. */
Expand Down Expand Up @@ -97,7 +104,12 @@ export const statementHoldFixture = Effect.gen(function* () {
if (hold.after !== undefined) {
const statement: Statement.Statement<unknown> = Reflect.apply(target, self, args);
const [text, bound] = statement.compile();
if (!text.includes(answeredChecks[hold.after.check]) || !bound.includes(hold.after.id))
if (hold.after.check === "kept-order") {
if (!text.includes(keptOrderPage)) return statement;
} else if (
!text.includes(answeredChecks[hold.after.check]) ||
!bound.includes(hold.after.id)
)
return statement;
// Disarmed by the first such check to answer, not to be built: one built and never run
// must not take the hold.
Expand Down Expand Up @@ -163,7 +175,12 @@ export const statementHoldFixture = Effect.gen(function* () {
yield* release;
armed = current = {
app: "app" in arm ? arm.app : undefined,
after: "after" in arm ? { check: arm.after, id: arm.id } : undefined,
after:
"id" in arm
? { check: arm.after, id: arm.id }
: "after" in arm
? { check: arm.after }
: undefined,
toolCall: "app" in arm && arm.toolCall === true,
failAccounts: "fail" in arm,
reached: yield* Deferred.make<void>(),
Expand Down
30 changes: 30 additions & 0 deletions e2e/test-plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8181,6 +8181,36 @@ export const scenarios = plan({
local: na("Hosted MCP scenario"),
},
},
mcpSearchKeptOrderEvicted: {
fixtures: "actors",
file: "mcp-search-kept-order-races.spec.ts",
title: "A held kept-order read cannot resurrect an evicted order or skew admission",
targets: {
"self-host": scheduled,
cloud: na("Needs the self-host test entry point's SQL hold and monotonic clock."),
local: na("Needs the self-host test entry point's SQL hold and monotonic clock."),
},
},
mcpSearchKeptOrderReplaced: {
fixtures: "actors",
file: "mcp-search-kept-order-races.spec.ts",
title: "A held kept-order read cannot overwrite a fresher order",
targets: {
"self-host": scheduled,
cloud: na("Needs the self-host test entry point's SQL hold."),
local: na("Needs the self-host test entry point's SQL hold."),
},
},
mcpSearchKeptOrderStale: {
fixtures: "actors",
file: "mcp-search-kept-order-races.spec.ts",
title: "A held kept-order read with a stale stamp cannot discard a fresher order",
targets: {
"self-host": scheduled,
cloud: na("Needs the self-host test entry point's SQL hold."),
local: na("Needs the self-host test entry point's SQL hold."),
},
},
mcpSearchStatements: {
fixtures: "actors",
file: "mcp-search-statements.spec.ts",
Expand Down
Loading
Loading