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: 1 addition & 1 deletion .agents/skills/test-t3-app/references/feature-map.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Code: apps/web/src/components/sidebar/
- **sidebar.working-section** — collapsed "Working" section for threads busy with background work. Turn off in **Settings → General → Working section**.
- **sidebar.snoozed-shelf** — "Snoozed" threads, hidden until their wake time.
- **sidebar.settled-shelf** — "Settled" finished threads. Drag a thread onto the header to settle it.
- **sidebar.bots-group** — "Bots" group for standing bot threads.
- **sidebar.bots-group** — "My Bots" and "Shared Bots" groups for standing bot threads. Shared bots are the ones filed to the Shared inbox.
- **sidebar.project-folder** — per-project folders when **Settings → Labs → Boring sidebar (group by project)** is on; "Pin project" / "Unpin project", "Project settings".
- **sidebar.thread-row** — one thread: title, status, PR badge, hover actions. Click opens `/$environmentId/$threadId`. Drop files on it to attach them.
- **sidebar.thread-status** — row status labels: "Working", "Waiting", "Needs you", "Needs action", "Usage limit reached", "Failed", "Woke", "Unsent draft".
Expand Down
5 changes: 0 additions & 5 deletions apps/mobile/src/state/threadOutboxDelivery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import * as Cause from "effect/Cause";
import { AsyncResult, Atom } from "effect/reactivity";

import { createDebugLogger } from "../lib/debugLog";
import { buildSeenMarker } from "@t3tools/client-runtime/seen-marker";
import { scopedThreadKey } from "../lib/scopedEntities";
import { buildProjectThreadStartTurnInput } from "../lib/projectThreadStartTurn";
import { serializeComposerMessageForServer, uploadedComposerContext } from "../lib/composerContext";
Expand Down Expand Up @@ -948,9 +947,6 @@ export function createThreadOutboxDelivery(): {
if (!isQueuedMessagePayloadCurrent(persistedMessage, deliveryRevision)) return true;
}
if (!hasAccess()) return true;
const seenMarker = buildSeenMarker(
scopedThreadKey(queuedMessage.environmentId, queuedMessage.threadId),
);
const deliveryResult = await runDeliveryCommand(threadEnvironment.startTurn, {
environmentId: queuedMessage.environmentId,
input: {
Expand Down Expand Up @@ -980,7 +976,6 @@ export function createThreadOutboxDelivery(): {
createdAt: queuedMessage.createdAt,
dispatchMode: queuedMessage.dispatchMode ?? "start",
...(queuedMessage.freshContext ? { freshContext: true } : {}),
...(seenMarker ? { seenMarker } : {}),
resendAfterSocketLoss: false,
},
});
Expand Down
51 changes: 45 additions & 6 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import type {
OrchestrationV2ThreadHistoryPage,
ProjectId,
} from "@t3tools/contracts";
import { makeThreadInbox, routeThreadInbox, type ThreadInbox } from "@t3tools/contracts";
import {
makeThreadInbox,
nextFounderSeenMessage,
nextFounderVisits,
rewindFounderVisit,
routeThreadInbox,
type ThreadInbox,
} from "@t3tools/contracts";
import { ServerEnvironmentIdentity } from "../environment/ServerEnvironment.ts";
import {
latestExecutedRun,
Expand Down Expand Up @@ -3937,6 +3944,23 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
const movesForward =
thread.lastVisitedAt === null ||
DateTime.toEpochMillis(visitedAt.value) > DateTime.toEpochMillis(thread.lastVisitedAt);
// A named founder on a shared thread moves only their unread cursor. The shared watermark
// stays, so the other founder's unread mark does not clear. The message they were shown is
// stored for any named founder and still starts no turn.
const namedFounder = command.founder;
const sharedFounder =
namedFounder !== undefined && thread.inbox?.audience === "shared" ? namedFounder : null;
const nextVisits =
sharedFounder === null
? undefined
: nextFounderVisits(thread, sharedFounder, visitedAt.value);
const founderMoved = nextVisits !== undefined && nextVisits !== thread.visitsByFounder;
const nextSeen =
namedFounder === undefined || command.lastSeenMessageId === undefined
? undefined
: nextFounderSeenMessage(thread, namedFounder, command.lastSeenMessageId);
const seenMoved = nextSeen !== undefined && nextSeen !== thread.seenMessageByFounder;
const globalMoves = sharedFounder === null && movesForward;
const occurredAt = yield* DateTime.now;
// Opens are timed by the server clock so devices with skewed clocks agree.
const opens = command.opened
Expand All @@ -3946,7 +3970,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
command.searchQuery,
)
: null;
if (!movesForward && opens === null) return;
if (!globalMoves && !founderMoved && !seenMoved && opens === null) return;
// Viewing a thread changes read state only. Loading its transcript, bumping
// updatedAt, or copying the thread into the event makes a read receipt scale with it.
yield* emit(
Expand All @@ -3958,8 +3982,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
providerInstanceId: thread.providerInstanceId,
occurredAt,
payload: {
lastVisitedAt: movesForward ? visitedAt.value : thread.lastVisitedAt,
lastVisitedAt: globalMoves ? visitedAt.value : thread.lastVisitedAt,
...(opens ? { opens } : {}),
...(founderMoved && nextVisits !== undefined ? { visitsByFounder: nextVisits } : {}),
...(seenMoved && nextSeen !== undefined ? { seenMessageByFounder: nextSeen } : {}),
},
});
});
Expand Down Expand Up @@ -4884,12 +4910,25 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
const snoozeExpired =
thread.snoozedUntil != null &&
DateTime.toEpochMillis(thread.snoozedUntil) <= DateTime.toEpochMillis(now);
const snoozeClear = snoozeExpired
? { snoozedUntil: null, snoozedAt: null, lastSnoozeWakeAt: thread.snoozedUntil }
: {};
// A named founder marking a shared thread unread rewinds only their cursor.
if (
command.founder !== undefined &&
thread.inbox?.audience === "shared" &&
markUnreadVisitedAt !== null
) {
return {
...thread,
visitsByFounder: rewindFounderVisit(thread, command.founder, markUnreadVisitedAt),
...snoozeClear,
};
}
return {
...thread,
lastVisitedAt: markUnreadVisitedAt,
...(snoozeExpired
? { snoozedUntil: null, snoozedAt: null, lastSnoozeWakeAt: thread.snoozedUntil }
: {}),
...snoozeClear,
};
}
case "thread.metadata.update": {
Expand Down
6 changes: 6 additions & 0 deletions apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,12 @@ export function threadShellFromProjection(
...(projection.thread.tags?.length ? { tags: projection.thread.tags } : {}),
...(projection.thread.standing ? { standing: projection.thread.standing } : {}),
lastVisitedAt: projection.thread.lastVisitedAt,
...(projection.thread.visitsByFounder === undefined
? {}
: { visitsByFounder: projection.thread.visitsByFounder }),
...(projection.thread.seenMessageByFounder === undefined
? {}
: { seenMessageByFounder: projection.thread.seenMessageByFounder }),
opens: projection.thread.opens ?? null,
titleRegeneration: projection.thread.titleRegeneration ?? null,
lastTitleRegeneration: projection.thread.lastTitleRegeneration ?? null,
Expand Down
15 changes: 10 additions & 5 deletions apps/server/src/orchestration-v2/ProviderTurnControlService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { projectComposerContextForProvider } from "@t3tools/shared/composerContextReferences";
import { formatSeenMarkerForProvider } from "@t3tools/shared/seenMarker";
import { formatSeenMarkerForProvider, formatSharedSenderForProvider } from "@t3tools/shared/seenMarker";
import {
MessageId,
type ModelSelection,
Expand Down Expand Up @@ -437,6 +437,7 @@ export const layer: Layer.Layer<
const loaded = yield* load({ ...input, operation: "steer" });
if (Option.isNone(loaded.session)) return;
const { message, run } = loaded.context;
const { thread } = yield* projections.getThreadRecords(input.threadId, []);
if (message === undefined || run === undefined) {
return yield* new ProviderTurnControlError({
threadId: input.threadId,
Expand All @@ -457,11 +458,15 @@ export const layer: Layer.Layer<
message: {
messageId: message.id,
text: formatSeenMarkerForProvider(
projectComposerContextForProvider({
text: message.text,
records: message.context?.records ?? [],
}),
formatSharedSenderForProvider(
projectComposerContextForProvider({
text: message.text,
records: message.context?.records ?? [],
}),
thread.inbox?.audience === "shared" ? (message.sentBy?.founder ?? null) : null,
),
message.seenMarker,
thread.inbox?.audience === "shared" ? (message.sentBy?.founder ?? null) : null,
),
attachments: message.attachments,
createdBy: message.createdBy,
Expand Down
16 changes: 11 additions & 5 deletions apps/server/src/orchestration-v2/ProviderTurnStartService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { modelSelectionsEqual } from "@t3tools/shared/model";
import { projectComposerContextForProvider } from "@t3tools/shared/composerContextReferences";
import { formatSeenMarkerForProvider } from "@t3tools/shared/seenMarker";
import { formatSeenMarkerForProvider, formatSharedSenderForProvider } from "@t3tools/shared/seenMarker";
import {
CommandId,
latestProviderTurnForAttempt,
Expand Down Expand Up @@ -911,12 +911,18 @@ export const layer: Layer.Layer<
});
let effectiveHandoffs = handoffs;
let resumeFellBack = false;
const sharedFounder =
projection.thread.inbox?.audience === "shared" ? (providerMessage.sentBy?.founder ?? null) : null;
const userText = formatSeenMarkerForProvider(
projectComposerContextForProvider({
text: providerMessage.text,
records: providerMessage.context?.records ?? [],
}),
formatSharedSenderForProvider(
projectComposerContextForProvider({
text: providerMessage.text,
records: providerMessage.context?.records ?? [],
}),
sharedFounder,
),
providerMessage.seenMarker,
sharedFounder,
);
// Native goal commands go verbatim; handoff history waits a turn.
const goalDriver =
Expand Down
42 changes: 35 additions & 7 deletions apps/server/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { OrchestrationDispatchCommandError } from "@t3tools/contracts";
// @effect-diagnostics-next-line nodeBuiltinImport:off -- A synchronous digest inside the pure provider mapping.
import * as NodeCrypto from "node:crypto";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { creditUnassignedSender } from "@t3tools/shared/seenMarker";
import * as Context from "effect/Context";
import * as NodeHttpServerRequest from "@effect/platform-node/NodeHttpServerRequest";
import * as SecureRandom from "effect/crypto/SecureRandom";
Expand Down Expand Up @@ -2030,6 +2031,20 @@ export const layerWsRpc = (
return yield* mutateProjectWithClone(projectService, projectCloneTracker, mutation);
});

// A session names a person only when another client assigned it. A person's unassigned
// client credits this machine's owner; a bot never does, and a null owner stays unnamed.
const messageSender = serverAuth.messageSenderOf(currentSessionId).pipe(
Effect.filterOrElse(
(sentBy) => sentBy.founder !== null || sentBy.deviceType === "bot",
(sentBy) =>
serverSettings.getSettings.pipe(
Effect.map((settings) => creditUnassignedSender(sentBy, settings.ownerFounder)),
// A settings read must not fail the send. Leave the sender unassigned.
Effect.orElseSucceed(() => sentBy),
),
),
);

const handlers = ServerWsRpcGroup.of({
[ORCHESTRATION_V2_WS_METHODS.dispatchCommand]: (command) =>
Effect.annotateCurrentSpan({
Expand All @@ -2049,15 +2064,15 @@ export const layerWsRpc = (
? { "orchestration_v2.source_thread_id": command.sourceThreadId }
: {}),
}).pipe(
Effect.andThen(
startup
Effect.andThen(() => {
const dispatched = startup
.enqueueCommand(
(command.type === "prepared-run.retry"
? threadLaunch.retryPreparation(command)
: (command.type === "message.dispatch" ||
command.type === "queued-run.edit" ||
command.type === "runtime-request.respond"
? serverAuth.messageSenderOf(currentSessionId).pipe(Effect.asSome)
? messageSender.pipe(Effect.asSome)
: Effect.succeed(Option.none<OrchestrationV2MessageSentBy>())
).pipe(
Effect.flatMap((sentBy) =>
Expand Down Expand Up @@ -2091,8 +2106,23 @@ export const layerWsRpc = (
cause,
});
}),
);
// A bot reading a thread writes no visit. A sender lookup that fails still records
// the visit, matching a client the server cannot name.
if (command.type !== "thread.visit") return dispatched;
return messageSender.pipe(
Effect.map((sentBy) => sentBy.deviceType === "bot"),
Effect.orElseSucceed(() => false),
Effect.flatMap((bot) =>
bot
? applicationEvents.latestApplicationSequence.pipe(
Effect.map((sequence) => ({ sequence })),
Effect.orElseSucceed(() => ({ sequence: 0 })),
)
: dispatched,
),
),
);
}),
),
[ORCHESTRATION_V2_WS_METHODS.stopWorkflow]: (input) => threadManagement.stopWorkflow(input),
[ORCHESTRATION_V2_WS_METHODS.forceStopThread]: (input) =>
Expand Down Expand Up @@ -2259,9 +2289,7 @@ export const layerWsRpc = (
})
.pipe(
Effect.andThen(
input.initialMessage === undefined
? Effect.undefined
: serverAuth.messageSenderOf(currentSessionId),
input.initialMessage === undefined ? Effect.undefined : messageSender,
),
Effect.flatMap((sentBy) =>
ThreadMessageIntake.launchThread({
Expand Down
9 changes: 7 additions & 2 deletions apps/web/src/components/ChatView.logic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { toServerClock } from "@t3tools/client-runtime/clock";
import { type OrchestrationV2ProviderTurn, type RunAttemptId } from "@t3tools/contracts";
import {
type Founder,
type OrchestrationV2ProviderTurn,
type RunAttemptId,
} from "@t3tools/contracts";
import {
deriveLatestThreadRun,
deriveThreadRuntime,
Expand Down Expand Up @@ -1459,6 +1463,7 @@ export function presentThreadShellFromProjection(
readonly updatedAt: ThreadProjection["updatedAt"];
readonly lastVisitedAt: ThreadProjection["thread"]["lastVisitedAt"];
},
viewer: Founder | null = null,
) {
const { updatedAt, lastVisitedAt } = live;
const latestRun = deriveLatestThreadRun(projection);
Expand Down Expand Up @@ -1494,7 +1499,7 @@ export function presentThreadShellFromProjection(
itemCount: projection.turnItems.length,
visibleItemCount: projection.visibleTurnItems.length,
updatedAt,
}),
}, null, viewer),
latestRun,
runtime,
};
Expand Down
Loading
Loading