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
4 changes: 3 additions & 1 deletion .github/fork-seams.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
]
},
"apps/server/src/serverSettings.ts": {
"why": "Every settings write goes through upstream's patch and write paths here. The fork swaps in its patch (fork/serverSettingsPatch.ts, under upstream's name, so a project's MCP profile a client left out is kept) and calls its save check (fork/serverSettingsWriteCheck.ts) once in each write path.",
"why": "Every settings write goes through upstream's patch and write paths here. The fork swaps in its patch (fork/serverSettingsPatch.ts, under upstream's name, so a project's MCP profile a client left out is kept) and calls its save check (fork/serverSettingsWriteCheck.ts) once in each write path. The load decodes the file after fork/settingsFileRead.ts (old auto-update names renamed, an unusable interval left out so it reads as its default).",
"added": [
"// Fork: upstream's patch keeping a project's MCP profile a client left out, and the save check.",
"import { applyServerSettingsPatchKeepingForkProjectKeys as applyServerSettingsPatch } from \"./fork/serverSettingsPatch.ts\";",
"import * as ForkSettingsWrite from \"./fork/serverSettingsWriteCheck.ts\";",
"import { prepareSettingsFile } from \"./fork/settingsFileRead.ts\"; // Fork: renames, bad intervals",
": decodePersistedServerSettingsExit(withDefaults(prepareSettingsFile(parsed.value)));",
"Effect.flatMap(ForkSettingsWrite.withCheck(update, \"test settings\")), // Fork: save check",
"yield* ForkSettingsWrite.check(current, updated, settingsPath); // Fork: save check"
]
Expand Down
1 change: 1 addition & 0 deletions FORK_CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ is missing here or a setting named here does not exist.
| Playtest mode, part 3: the worker renews claims, ends idle, too-long and gone-thread sessions, finishes what a restart cut, and tells the thread once when T3 ended a session. | A person tries an agent's unfinished work on a test target, round after round, no PR per round. | `server.playtest`, `projectOverride.playtest` | off | [#262](https://github.com/scuttletoby/t3code/pull/262) |
| Handoff instead of compaction, part 1: the setting and the pure "should this thread hand off now?" decision. Nothing acts on it yet. | Long threads keep their children, PRs and waits instead of compacting. | `server.handoff`, `projectOverride.handoff` | off | [#282](https://github.com/scuttletoby/t3code/pull/282) |
| Review rules, part 1: the setting for how this machine runs a review lens (runner, state question), off by default; `{"preset": "recommended"}` turns the fork's defaults on. A bad hand edit reads as off and is kept. Nothing reads it yet. | The repo says what a pull request needs and CI enforces it; the machine says how reviews run. | `server.reviewRules`, `projectOverride.reviewRules` | off | [#291](https://github.com/scuttletoby/t3code/pull/291) |
| An interval setting the settings file cannot hold (negative, not finite, too large) is refused when saved; one already in the file reads as its default instead of resetting every setting. A test finds every interval setting from the schema. | A `1e400` saved as `null` made the next start reject the whole settings file. | none | always | [#323](https://github.com/scuttletoby/t3code/pull/323) |

## Repository, CI and releases

Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/fork/serverSettingsWriteCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import * as HostProcess from "@t3tools/shared/HostProcess";
import { Effect } from "effect";

/** Fails when saving `next` over `current` would bring in a fork setting that cannot apply. */
/** Fails when saving `next` over `current` would bring in a fork setting or interval that cannot apply. */
export function check(
current: ServerSettings,
next: ServerSettings,
Expand Down
23 changes: 23 additions & 0 deletions apps/server/src/fork/settingsFileRead.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Fork: the parsed settings file before upstream's decode: old auto-update names renamed, and an
* unusable interval left out with a warning, so it reads as its default instead of failing the
* whole file (which resets every setting). The next save removes it from the file.
*/
import { dropUnusableStoredIntervals } from "@t3tools/shared/forkSettingsPatch";
import * as Effect from "effect/Effect";

import { renameRetiredAutoUpdateKeys } from "../cloud/autoUpdateSettings.ts";

export function prepareSettingsFile(parsed: unknown): unknown {
const { settings, dropped } = dropUnusableStoredIntervals(renameRetiredAutoUpdateKeys(parsed));
if (dropped.length > 0) {
// The decode is synchronous; its own warnings (wait checks) are logged the same way.
Effect.runSync(
Effect.logWarning(
"settings: unusable intervals in settings.json read as their defaults; the next settings save removes them from the file",
{ dropped },
),
);
}
return settings;
}
247 changes: 247 additions & 0 deletions apps/server/src/serverSettings.forkIntervals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
// Fork: interval (Duration) settings. Every one, found by walking the settings schema, survives a
// patch and a save and load (#45: upstream's patch merge and save each list them by hand). A save
// refuses an interval no run could use, and a file that holds one loads with that interval at its
// default instead of failing whole (#46).

import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import {
DEFAULT_SERVER_SETTINGS,
ServerSettings,
ServerSettingsError,
ServerSettingsPatch,
} from "@t3tools/contracts";
import {
findDurationPaths,
ForkSettingsWriteError,
SETTINGS_INTERVAL_PATHS,
} from "@t3tools/shared/forkSettingsPatch";
import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
import { assert, it } from "@effect/vitest";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";

import * as ServerSecretStore from "./auth/ServerSecretStore.ts";
import * as ServerConfig from "./config.ts";
import * as SqlitePersistence from "./persistence/Sqlite.ts";
import * as ManagedPolicy from "./policy/ManagedPolicy.ts";
import * as ServerSettingsModule from "./serverSettings.ts";

const layerServerSettings = () =>
ServerSettingsModule.layer.pipe(
Layer.provide(ServerSecretStore.layer),
Layer.provide(ManagedPolicy.layerTest()),
Layer.provideMerge(Layer.fresh(SqlitePersistence.layerMemory)),
Layer.provideMerge(
Layer.fresh(ServerConfig.layerTest(process.cwd(), { prefix: "t3code-fork-intervals-test-" })),
),
);

const isRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null && !Array.isArray(value);

const getPath = (value: unknown, path: ReadonlyArray<string>): unknown =>
path.reduce<unknown>((node, key) => (isRecord(node) ? node[key] : undefined), value);

const setPath = (value: unknown, path: ReadonlyArray<string>, leaf: unknown): unknown => {
const [key, ...rest] = path;
if (key === undefined) return leaf;
const node = isRecord(value) ? value : {};
return { ...node, [key]: setPath(node[key], rest, leaf) };
};

const millisAt = (settings: ServerSettings, path: ReadonlyArray<string>): number | undefined => {
const value = getPath(settings, path);
return Duration.isDuration(value) ? Duration.toMillis(value) : undefined;
};

/** What else a patch needs for an interval under this top key to apply. */
const PATCH_CONTEXT: Readonly<Record<string, unknown>> = {
// Overrides apply only to a custom profile.
backgroundActivity: { profile: "custom" },
};

/** A patch, as a client sends it, that sets the interval at `path` to `millis`. */
const decodePatch = Schema.decodeUnknownSync(ServerSettingsPatch);

const patchSetting = (path: ReadonlyArray<string>, millis: number): ServerSettingsPatch =>
decodePatch(setPath({ [path[0]!]: PATCH_CONTEXT[path[0]!] }, path, millis));

const readFileText = Effect.gen(function* () {
const config = yield* ServerConfig.ServerConfig;
const fs = yield* FileSystem.FileSystem;
return yield* fs.readFileString(config.settingsPath);
});

const writeFileText = (text: string) =>
Effect.gen(function* () {
const config = yield* ServerConfig.ServerConfig;
const fs = yield* FileSystem.FileSystem;
yield* fs.writeFileString(config.settingsPath, text);
});

const decodeFile = (text: string): ServerSettings => {
const decoded = ServerSettingsModule.decodeSettingsFileText(text);
if (Exit.isFailure(decoded)) assert.fail(`the file does not decode: ${text}`);
return decoded.value;
};

const SET_MILLIS = 123_456;
const pathName = (path: ReadonlyArray<string>) => path.join(".");

it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))(
"interval settings found from the schema (#45)",
(it) => {
it("finds every interval, each at a key path", () => {
const { paths, outOfReach } = findDurationPaths(ServerSettings);
// An interval inside a record or array has no fixed path for the checks below to follow.
assert.deepStrictEqual(outOfReach, []);
for (const known of [
"automaticGitFetchInterval",
"providerHealthRefreshInterval",
"backgroundActivity.overrides.hostPowerMonitorIdleInterval",
"headlessUsageRefreshInterval",
]) {
assert.include(paths.map(pathName), known);
}
// No default is the value set below, so each check sees a change.
for (const path of paths)
assert.notStrictEqual(millisAt(DEFAULT_SERVER_SETTINGS, path), SET_MILLIS);
});

it.each(SETTINGS_INTERVAL_PATHS.map((path) => ({ name: pathName(path), path })))(
"keeps $name through a patch",
({ path }) => {
const patched = applyServerSettingsPatch(
DEFAULT_SERVER_SETTINGS,
patchSetting(path, SET_MILLIS),
);
assert.strictEqual(millisAt(patched, path), SET_MILLIS);
},
);

it.effect.each(SETTINGS_INTERVAL_PATHS.map((path) => ({ name: pathName(path), path })))(
"keeps $name through a save and the next load",
({ path }) =>
Effect.gen(function* () {
yield* writeFileText("{}");
const service = yield* ServerSettingsModule.ServerSettingsService;
const saved = yield* service.updateSettings(patchSetting(path, SET_MILLIS));
assert.strictEqual(millisAt(saved, path), SET_MILLIS);
const text = yield* readFileText;
assert.strictEqual(getPath(JSON.parse(text), path), SET_MILLIS);
assert.strictEqual(millisAt(decodeFile(text), path), SET_MILLIS);
}).pipe(Effect.provide(layerServerSettings())),
);
},
);

it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))(
"an interval no run could use (#46)",
(it) => {
const REFUSED = [
// A client's JSON can say 1e400, which parses as Infinity and would be saved as null.
{
name: "1e400",
path: ["providerHealthRefreshInterval"],
millis: JSON.parse("1e400") as number,
},
{ name: "a negative value", path: ["headlessUsageRefreshInterval"], millis: -5 },
{
name: "a value past the largest",
path: ["backgroundActivity", "overrides", "hostPowerMonitorActiveInterval"],
millis: 1e300,
},
];

it.effect.each(REFUSED)(
"refuses a save of $name and leaves the file as it was",
({ path, millis }) =>
Effect.gen(function* () {
yield* writeFileText('{"enableAgentBrowserAccess": false}');
const service = yield* ServerSettingsModule.ServerSettingsService;
yield* service.getSettings;
const patch = patchSetting(path, millis);
const failure = yield* Effect.flip(service.updateSettings(patch));
assert.instanceOf(failure, ServerSettingsError);
assert.instanceOf(failure.cause, ForkSettingsWriteError);
assert.include((failure.cause as ForkSettingsWriteError).problem, pathName(path));
assert.strictEqual(yield* readFileText, '{"enableAgentBrowserAccess": false}');
}).pipe(Effect.provide(layerServerSettings())),
);

// As a hand edit or an older server's save of 1e400 leaves it.
const BAD_FILE = `{
"enableAgentBrowserAccess": false,
"automaticGitFetchInterval": null,
"headlessUsageRefreshInterval": "10m",
"backgroundActivity": {
"profile": "custom",
"overrides": {
"providerHealthRefreshInterval": 90000,
"hostPowerMonitorActiveInterval": -5,
"hostPowerMonitorIdleInterval": 1e400
}
}
}`;

it.effect(
"loads a file holding bad intervals with those at their defaults and the rest kept",
() =>
Effect.gen(function* () {
yield* writeFileText(BAD_FILE);
const service = yield* ServerSettingsModule.ServerSettingsService;
const loaded = yield* service.getSettings;
// The rest of the file is read, not reset to every default.
assert.strictEqual(loaded.enableAgentBrowserAccess, false);
assert.strictEqual(
millisAt(loaded, ["backgroundActivity", "overrides", "providerHealthRefreshInterval"]),
90_000,
);
for (const path of [
["headlessUsageRefreshInterval"],
["backgroundActivity", "overrides", "hostPowerMonitorActiveInterval"],
["backgroundActivity", "overrides", "hostPowerMonitorIdleInterval"],
]) {
assert.strictEqual(
millisAt(loaded, path),
millisAt(DEFAULT_SERVER_SETTINGS, path),
pathName(path),
);
}

// Loaded again before any save (a start that stopped before saving): the same result.
assert.deepStrictEqual(decodeFile(BAD_FILE), decodeFile(BAD_FILE));
assert.strictEqual(decodeFile(BAD_FILE).enableAgentBrowserAccess, false);

// A save is not refused, and leaves a file with no bad interval that loads as it was saved.
yield* service.updateSettings({ enableAgentBrowserAccess: true });
const text = yield* readFileText;
const file = JSON.parse(text) as unknown;
assert.notInclude(text, "null");
assert.isUndefined(getPath(file, ["headlessUsageRefreshInterval"]));
assert.isUndefined(
getPath(file, ["backgroundActivity", "overrides", "hostPowerMonitorIdleInterval"]),
);
assert.strictEqual(
getPath(file, ["backgroundActivity", "overrides", "providerHealthRefreshInterval"]),
90_000,
);
const reloaded = decodeFile(text);
assert.strictEqual(reloaded.enableAgentBrowserAccess, true);
assert.strictEqual(
millisAt(reloaded, [
"backgroundActivity",
"overrides",
"providerHealthRefreshInterval",
]),
90_000,
);
}).pipe(Effect.provide(layerServerSettings())),
);
},
);
3 changes: 2 additions & 1 deletion apps/server/src/serverSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import * as ServerConfig from "./config.ts";
import { type DeepPartial, deepMerge } from "@t3tools/shared/Struct";
import { fromJsonStringPretty, fromLenientJson } from "@t3tools/shared/schemaJson";
import { renameRetiredAutoUpdateKeys } from "./cloud/autoUpdateSettings.ts";
import { prepareSettingsFile } from "./fork/settingsFileRead.ts"; // Fork: renames, bad intervals
import {
deriveLegacyProjectOverrides,
fallbackTextGenerationModelSelection,
Expand Down Expand Up @@ -403,7 +404,7 @@ export function decodeSettingsFileText(
const parsed = decodeSettingsFileJsonExit(raw);
return Exit.isFailure(parsed)
? parsed
: decodePersistedServerSettingsExit(withDefaults(renameRetiredAutoUpdateKeys(parsed.value)));
: decodePersistedServerSettingsExit(withDefaults(prepareSettingsFile(parsed.value)));
}

/** The load's lenient parse of a settings file's text (comments, trailing commas), before decoding. */
Expand Down
Loading
Loading