Skip to content

Commit 33042a4

Browse files
Merge branch 'main' into submit/engine-range-whitespace
2 parents 6f535af + a5e7e5b commit 33042a4

13 files changed

Lines changed: 442 additions & 99 deletions

‎apps/server/src/cli/thread.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { assert, it } from "@effect/vitest";
22
import { RunId, ThreadId } from "@t3tools/contracts";
33
import * as Effect from "effect/Effect";
44

5-
import { readLatestMessages, type ThreadTools, waitForRun } from "./thread.ts";
5+
import { readLatestMessages, type ThreadTools, titleFromMessage, waitForRun } from "./thread.ts";
66

77
const threadId = ThreadId.make("thread-1");
88
const runId = RunId.make("run-1");
@@ -160,3 +160,8 @@ it.effect("show returns a --messages count larger than one call can spread", ()
160160
);
161161
}),
162162
);
163+
164+
it("titles from a long first line without splitting an emoji", () => {
165+
const title = titleFromMessage(`${"a".repeat(78)}😀 and the rest of a long first line`);
166+
assert.strictEqual(title, `${"a".repeat(78)}…`);
167+
});

‎apps/server/src/cli/thread.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,16 @@ const finishRun = Effect.fn("cli.thread.finishRun")(function* (
508508
}
509509
});
510510

511-
const titleFromMessage = (message: string) => {
511+
export const titleFromMessage = (message: string) => {
512512
const firstLine = message.trim().split("\n")[0]?.trim() ?? "";
513-
return firstLine.length > TITLE_MAX_CHARS
514-
? `${firstLine.slice(0, TITLE_MAX_CHARS - 1).trimEnd()}…`
515-
: firstLine;
513+
if (firstLine.length <= TITLE_MAX_CHARS) return firstLine;
514+
let end = TITLE_MAX_CHARS - 1;
515+
const previous = firstLine.charCodeAt(end - 1);
516+
const next = firstLine.charCodeAt(end);
517+
if (previous >= 0xd800 && previous <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) {
518+
end -= 1;
519+
}
520+
return `${firstLine.slice(0, end).trimEnd()}…`;
516521
};
517522

518523
const launchWorkspaceStrategy = (input: {

‎apps/server/src/cloud/bootService.test.ts‎

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,19 @@ const macInstallerPath =
9090
const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
9191
platform: NodeJS.Platform = "linux",
9292
installerPath = macInstallerPath,
93+
xdgConfigHome?: string,
94+
managerXdgConfigHome = xdgConfigHome,
9395
) {
9496
const fs = yield* FileSystem.FileSystem;
9597
const path = yield* Path.Path;
9698
const home = yield* fs.makeTempDirectoryScoped({ prefix: "t3-boot-service-test-" });
99+
const managerUnitDir = path.join(
100+
managerXdgConfigHome !== undefined && path.isAbsolute(managerXdgConfigHome)
101+
? managerXdgConfigHome
102+
: path.join(home, ".config"),
103+
"systemd",
104+
"user",
105+
);
97106
const baseDir = path.join(home, ".t3");
98107
const statePath = path.join(baseDir, "runtime", "service-state.json");
99108
const runtime = pinnedRuntimePaths(path, baseDir, "1.2.3", platform);
@@ -140,15 +149,26 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
140149
}
141150
return {
142151
stdout:
143-
input.args[0] === "--version"
144-
? `t3 v${/versions\/([^/]+)\//.exec(input.command)?.[1] ?? "1.2.3"}\n`
145-
: input.command === "loginctl" && input.args[0] === "show-user"
146-
? `${control.linger}\n`
147-
: input.args[1] === "is-enabled"
148-
? control.enabled
149-
? "enabled\n"
150-
: "disabled\n"
151-
: "",
152+
input.command === "busctl"
153+
? JSON.stringify({
154+
type: "as",
155+
data: [
156+
"/usr/lib/systemd/user",
157+
`${managerUnitDir}.control`,
158+
"/run/user/501/systemd/transient",
159+
managerUnitDir,
160+
"/etc/systemd/user",
161+
],
162+
})
163+
: input.args[0] === "--version"
164+
? `t3 v${/versions\/([^/]+)\//.exec(input.command)?.[1] ?? "1.2.3"}\n`
165+
: input.command === "loginctl" && input.args[0] === "show-user"
166+
? `${control.linger}\n`
167+
: input.args[1] === "is-enabled"
168+
? control.enabled
169+
? "enabled\n"
170+
: "disabled\n"
171+
: "",
152172
stderr: "",
153173
code: ChildProcessSpawner.ExitCode(
154174
failed ||
@@ -198,6 +218,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
198218
ConfigProvider.fromEnv({
199219
env: {
200220
HOME: home,
221+
...(xdgConfigHome === undefined ? {} : { XDG_CONFIG_HOME: xdgConfigHome }),
201222
...(environmentPath === undefined || environmentPath === ""
202223
? {}
203224
: { PATH: environmentPath }),
@@ -213,6 +234,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* (
213234
service,
214235
makeService,
215236
fs,
237+
home,
216238
baseDir,
217239
statePath,
218240
commands,
@@ -315,10 +337,52 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
315337
}),
316338
);
317339

318-
it.effect("installs, reports current state, and uninstalls", () =>
340+
it.effect.each([
341+
"unset",
342+
"empty",
343+
"relative",
344+
"relocated",
345+
"shell-only",
346+
"manager-only",
347+
"manager-unavailable",
348+
] as const)("installs, discovers, restarts, and uninstalls with $XDG_CONFIG_HOME %s", (setting) =>
319349
Effect.gen(function* () {
320-
const { service, fs, statePath, timeouts, runtime } = yield* makeHarness();
350+
const path = yield* Path.Path;
351+
const fs = yield* FileSystem.FileSystem;
352+
const configRoot = yield* fs.makeTempDirectoryScoped({ prefix: "t3-xdg-config-" });
353+
const xdgConfigHome = {
354+
unset: undefined,
355+
empty: "",
356+
relative: "relative-config",
357+
relocated: path.join(configRoot, "config root"),
358+
"shell-only": path.join(configRoot, "config root"),
359+
"manager-only": undefined,
360+
"manager-unavailable": path.join(configRoot, "config root"),
361+
}[setting];
362+
const managerXdgConfigHome =
363+
setting === "shell-only"
364+
? ""
365+
: setting === "manager-only"
366+
? path.join(configRoot, "config root")
367+
: undefined;
368+
const { service, makeService, home, statePath, timeouts, runtime, control } =
369+
yield* makeHarness("linux", undefined, xdgConfigHome, managerXdgConfigHome);
370+
const unitPath = path.join(
371+
setting === "relocated" || setting === "manager-only" || setting === "manager-unavailable"
372+
? path.join(configRoot, "config root")
373+
: path.join(home, ".config"),
374+
"systemd",
375+
"user",
376+
"t3code.service",
377+
);
378+
expect(yield* service.status).toMatchObject({ installed: false, unitPath });
321379
const plan = yield* service.install();
380+
expect(plan.unitPath).toBe(unitPath);
381+
if (setting === "manager-unavailable") {
382+
control.failCommand =
383+
"busctl --user --json=short get-property org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager UnitPath";
384+
}
385+
const discoveredService = yield* makeService();
322386

323387
expect(parseServiceState(yield* fs.readFileString(statePath))).toEqual({
324388
protocol: SERVICE_LAUNCHER_PROTOCOL,
@@ -332,6 +396,12 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
332396
current: true,
333397
installedVersion: "1.2.3",
334398
});
399+
expect(yield* discoveredService.status).toMatchObject({
400+
installed: true,
401+
current: true,
402+
unitPath,
403+
});
404+
expect(yield* discoveredService.restart).toBe("restarted");
335405
const pendingState = JSON.stringify({
336406
protocol: SERVICE_LAUNCHER_PROTOCOL,
337407
activeVersion: "1.2.3",
@@ -345,8 +415,9 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
345415
});
346416
yield* fs.writeFileString(statePath, pendingState);
347417
expect((yield* service.status).current).toBe(false);
348-
expect(yield* service.uninstall).toBe(true);
349-
expect((yield* service.status).installed).toBe(false);
418+
expect(yield* discoveredService.uninstall).toBe(true);
419+
expect(yield* fs.exists(unitPath)).toBe(false);
420+
expect((yield* discoveredService.status).installed).toBe(false);
350421
// systemd can take 90s to stop, exceeding the runner's 60s default.
351422
expect(timeouts.get("systemctl --user disable --now t3code.service")).toEqual(
352423
Duration.seconds(120),

‎apps/server/src/cloud/bootService.ts‎

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,9 @@ export interface BootServiceManager {
213213

214214
function systemdManager(input: {
215215
readonly path: Path.Path;
216-
readonly homeDir: string;
216+
readonly unitDir: string;
217217
}): BootServiceManager {
218-
const unitPath = input.path.join(
219-
input.homeDir,
220-
".config",
221-
"systemd",
222-
"user",
223-
BOOT_SERVICE_UNIT_FILE,
224-
);
218+
const unitPath = input.path.join(input.unitDir, BOOT_SERVICE_UNIT_FILE);
225219
const prepareActivation = [
226220
{
227221
step: "reloading systemd user units",
@@ -358,6 +352,8 @@ function launchdManager(input: {
358352
function selectBootServiceManager(input: {
359353
readonly platform: NodeJS.Platform;
360354
readonly homeDir: string;
355+
readonly xdgConfigHome: string;
356+
readonly systemdUnitDir: string | undefined;
361357
readonly uid: number | undefined;
362358
readonly path: Path.Path;
363359
readonly environmentPath: string;
@@ -366,7 +362,18 @@ function selectBootServiceManager(input: {
366362
return undefined;
367363
}
368364
if (input.platform === "linux") {
369-
return systemdManager({ path: input.path, homeDir: input.homeDir });
365+
return systemdManager({
366+
path: input.path,
367+
unitDir:
368+
input.systemdUnitDir ??
369+
input.path.join(
370+
input.path.isAbsolute(input.xdgConfigHome)
371+
? input.xdgConfigHome
372+
: input.path.join(input.homeDir, ".config"),
373+
"systemd",
374+
"user",
375+
),
376+
});
370377
}
371378
if (input.platform === "darwin" && input.uid !== undefined) {
372379
return launchdManager({
@@ -379,6 +386,42 @@ function selectBootServiceManager(input: {
379386
return undefined;
380387
}
381388

389+
const decodeSystemdUnitPaths = Schema.decodeUnknownEffect(
390+
Schema.fromJsonString(Schema.Struct({ data: Schema.Array(Schema.String) })),
391+
);
392+
393+
// The manager may not share the installer's environment; JSON preserves spaces in its paths.
394+
const readSystemdUnitDirectory = Effect.fn("cloud.boot_service.read_systemd_unit_directory")(
395+
function* () {
396+
const runner = yield* ProcessRunner.ProcessRunner;
397+
const path = yield* Path.Path;
398+
const result = yield* runner.run({
399+
command: "busctl",
400+
args: [
401+
"--user",
402+
"--json=short",
403+
"get-property",
404+
"org.freedesktop.systemd1",
405+
"/org/freedesktop/systemd1",
406+
"org.freedesktop.systemd1.Manager",
407+
"UnitPath",
408+
],
409+
timeout: Duration.seconds(5),
410+
});
411+
if (result.code !== 0) return undefined;
412+
const { data } = yield* decodeSystemdUnitPaths(result.stdout);
413+
// The persistent control directory identifies the manager's config root despite UnitPath overrides.
414+
const controlDirectory = data.find(
415+
(directory) =>
416+
path.isAbsolute(directory) &&
417+
directory.endsWith("/systemd/user.control") &&
418+
data.includes(directory.slice(0, -".control".length)),
419+
);
420+
return controlDirectory?.slice(0, -".control".length);
421+
},
422+
Effect.orElseSucceed(() => undefined),
423+
);
424+
382425
export class BootServiceUnsupportedError extends Schema.TaggedError<BootServiceUnsupportedError>()(
383426
"BootServiceUnsupportedError",
384427
{ platform: Schema.String },
@@ -536,6 +579,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
536579
yield* Config.String(CLI_RELEASE_BASE_URL_ENV).pipe(Config.option),
537580
);
538581
const homeDir = yield* Config.String("HOME").pipe(Config.withDefault(""));
582+
const xdgConfigHome = yield* Config.String("XDG_CONFIG_HOME").pipe(Config.withDefault(""));
539583
const installerPath = yield* Config.String("PATH").pipe(Config.withDefault(""));
540584
const runningInsideService =
541585
(yield* Config.String(BOOT_SERVICE_UNIT_ENV).pipe(Config.withDefault(""))) ===
@@ -573,9 +617,12 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
573617
),
574618
).join(":");
575619

620+
const systemdUnitDir = platform === "linux" ? yield* readSystemdUnitDirectory() : undefined;
576621
const detectedManager = selectBootServiceManager({
577622
platform,
578623
homeDir,
624+
xdgConfigHome,
625+
systemdUnitDir,
579626
uid,
580627
path,
581628
environmentPath,

‎apps/server/src/orchestration-v2/ContextHandoffService.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,12 @@ it.effect("caps the side chat context block without summarizing discarded parent
169169
...importedItem({
170170
role: index % 2 === 0 ? "user" : "assistant",
171171
id: `parent-${index}`,
172-
text: `item-${index} ${"x".repeat(300)}`,
172+
text: `item-${index} ${"x".repeat(228)}😀${"x".repeat(70)}`,
173173
ordinal: index,
174174
}),
175175
get text() {
176176
summarizedItems++;
177-
return `item-${index} ${"x".repeat(300)}`;
177+
return `item-${index} ${"x".repeat(228)}😀${"x".repeat(70)}`;
178178
},
179179
}));
180180
const service = yield* ContextHandoffService.ContextHandoffServiceV2;
@@ -197,6 +197,7 @@ it.effect("caps the side chat context block without summarizing discarded parent
197197
assert.notInclude(context, "item-0 ");
198198
assert.include(context, "- Assistant: item-59 ");
199199
assert.isAtMost(context.length, 8_000);
200+
assert.isTrue(context.isWellFormed());
200201
assert.isTrue(
201202
context.endsWith(
202203
`Use t3_thread_read on thread ${parentThreadId} to see the main thread's live state.`,

‎apps/server/src/orchestration-v2/ContextHandoffService.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ function compactText(text: string, maxLength = 240): string {
109109
if (compacted.length <= maxLength) {
110110
return compacted;
111111
}
112-
return `${compacted.slice(0, maxLength - 3)}...`;
112+
const end = maxLength - 3;
113+
const last = compacted.charCodeAt(end - 1);
114+
return `${compacted.slice(0, last >= 0xd800 && last <= 0xdbff ? end - 1 : end)}...`;
113115
}
114116

115117
export function visibleDeltaRunOrdinals(

0 commit comments

Comments
 (0)