Skip to content

Record owners when agents launch threads - #753

Merged
lastobelus merged 6 commits into
lastcode/mainfrom
lastcode/thread-owner-record
Nov 19, 2026
Merged

lastobelus merged 6 commits into
lastcode/mainfrom
lastcode/thread-owner-record

Conversation

@lastobelus

@lastobelus lastobelus commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Part of #751

Sidebar placement (groupedUnderThreadId) can't say which thread owns a launched thread. Agent launches now record that owner when the thread is created, so the sidebar and archive work can read it directly. A user-requested top-level thread keeps its creator and starts with no owner.

Storage

New launches store:

creatorOrigin: { kind: "thread_launch", environmentId, threadId, owned?: boolean }
  • owned: true records a new owned launch. owned: false records a new explicitly top-level launch.
  • Historical repair keeps the original provenance shape, with no owned.
  • Promote (future): it can set lineage.independent and keep the original creator.
  • Clients can't set it. The server derives creatorOrigin and strips it from client thread.create commands.

Reading the owner

getThreadOwner(thread, environmentId) in packages/shared/src/threadOwnership.ts returns the environment-qualified owner. First match wins:

  1. No owner if lineage.independent is true.
  2. Remote delegation: delegatedFrom.
  3. Local subagent lineage: the parent thread, in this environment.
  4. New launch marker: creatorOrigin with kind: "thread_launch" and owned: true.

It ignores placement and historical creator fields.

Launch paths

Path Owner recorded Placement
Same-project launch The launching thread Keeps its existing default
Cross-project launch The same qualified owner No placement parent needed
Linked-environment launch The authenticated sender's identity No placement parent needed
Batch rows, with or without a request key Same rule as single launches Same rule as single launches
topLevel: true (either tool) Creator kept in provenance, owned: false Ungrouped and independent
Scheduled thread None Independent
  • Linked launches reserve delegatedFrom for delegated execution. It must match the linked sender when one is provided, or the call fails with invalid_request.
  • topLevel: true fails with invalid_request if combined with a non-null groupedUnderThreadId. On the project launch tool it also fails with remotePreparation or a linked caller's delegatedFrom.
  • Scheduled threads store the schedule ID in the new optional scheduledTaskOrigin. The scheduled task record gives that ID and may have no creator thread. Keeping the field optional preserves the existing creator-origin wire shape for older clients.

Worktree suspension

  • Legacy launches: classified the same as before.
  • New launches: an owned launch counts as an untouched-child candidate. An explicitly top-level launch counts under Other threads.
  • Typing in a thread: still protects its workspace, without releasing ownership.

Proof

The 405-test local run passed. After the suspension repair, the four classification cases and 22 launch, ownership, and context-budget tests passed. The explicit top-level model eval went 0/3 to 3/3, with 3/3 each on the default-ownership and thread-organization cases. The refactor passed 54 focused tests, including the existing handler tests, plus server typecheck, format, and scoped lint. Existing launch guidance was restored because there was no matched base sweep for the earlier cuts. The tool list is 85,056 bytes, up 396 from lastcode/main.

Local review: 4 rounds with GPT-6.1 Terra (xhigh), approved.

Upstream-owned files

The single and batch handlers call the fork-owned apps/server/src/mcp/threadLaunchOwnership.ts for ownership, placement and validation. The following files exist on origin/main. Counts are for the full PR diff against lastcode/main.

File Added / removed lines
apps/server/src/mcp/OrchestratorMcpService.ts +10 / −9
apps/server/src/mcp/linkOrigin.test.ts +44 / −0
apps/server/src/mcp/toolkits/orchestrator/tools.ts +2 / −2
apps/server/src/mcp/toolkits/project/handlers.test.ts +3 / −0
apps/server/src/mcp/toolkits/project/handlers.ts +10 / −11
apps/server/src/mcp/toolkits/project/tools.ts +2 / −1
apps/server/src/mcp/toolkits/thread/tools.ts +1 / −1
apps/server/src/orchestration-v2/Orchestrator.ts +4 / −0
apps/server/src/orchestration-v2/ProjectionStore.ts +6 / −0
apps/server/src/orchestration-v2/ThreadForkService.ts +1 / −0
apps/server/src/orchestration-v2/ThreadLaunchService.test.ts +4 / −0
apps/server/src/orchestration-v2/ThreadLaunchService.ts +4 / −0
apps/server/src/orchestration-v2/ThreadManagementService.test.ts +7 / −0
apps/server/src/orchestration-v2/ThreadManagementService.ts +2 / −1
apps/server/src/provider/agentContextBudget.json +1 / −1
apps/server/src/storageCleanup.test.ts +7 / −0
packages/contracts/src/orchestrationV2.ts +28 / −10
packages/contracts/src/orchestratorMcp.ts +1 / −0
packages/provider-core/src/server/subagentProjection.ts +1 / −0
packages/shared/package.json +4 / −0

@lastobelus
lastobelus marked this pull request as ready for review November 19, 2026 21:51
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T23:17:12.767085Z 8047b29 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@lastobelus

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: ab1d0f5043

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@lastobelus

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5636a80cfc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@lastobelus

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 370bf7c3f7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@lastobelus

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: cd2b4d8f91

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@lastobelus

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: f78a4f3dc9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Agent-created threads could appear as independent roots because launch provenance and ownership were missing. Store the exact creator and an opt-out for explicitly requested roots, and record schedule provenance at creation.

Carry-Group: incubator
Carry-Fix: #751
Carry-Observation: Keep launch ownership with the cross-environment creation and lifecycle contracts.

Implemented by GPT-6.2 Sol in Codex through T3 Code.
Regenerate the agent-context budget for the launch tool guidance and use a typed schema decoder in the compatibility test.

Carry-Group: incubator
Carry-Fix: #751

Implemented by GPT-6.2 Sol in Codex through T3 Code.
Keep the owner and placement rules in the launch tools while shortening their descriptions enough to fit the current context budget.

Carry-Group: incubator
Carry-Fix: #751

Implemented by GPT-6.2 Sol in Codex through T3 Code.
Record false for new independent launches so worktree suspension can classify them as other threads. Keep historical provenance without an ownership decision and accept explicit false in the ownership eval.

Carry-Group: incubator
Carry-Fix: #751

Implemented by GPT-6.2 Sol in Codex through T3 Code.
Hoist the compatibility test decoder to satisfy the schema lint rule.

Carry-Group: incubator
Carry-Fix: #751

Implemented by GPT-6.2 Sol in Codex through T3 Code.
Keep the launch and batch handlers thin by resolving ownership and placement in a fork-owned module. Restore the existing launch guidance alongside the new ownership rule.

Carry-Group: incubator
Carry-Fix: #751

Implemented by GPT-6.2 Sol in Codex through T3 Code.
@lastobelus
lastobelus force-pushed the lastcode/thread-owner-record branch from f78a4f3 to 8047b29 Compare November 19, 2026 23:14
@lastobelus

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 8047b293f3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@lastobelus lastobelus added the agent-context-increase Intentional agent context growth approved by a maintainer label Nov 19, 2026
@lastobelus
lastobelus merged commit 7b9aaaa into lastcode/main Nov 19, 2026
23 of 24 checks passed
@lastobelus
lastobelus deleted the lastcode/thread-owner-record branch November 19, 2026 23:32
lastobelus added a commit that referenced this pull request Nov 20, 2026
Part of #751

## Problem

Agents launch threads in other projects, and #753 now records who owns them. Archiving the parent at wrap-up still left that owned work open, so it had to be archived thread by thread. This PR makes wrap-up follow ownership across projects, and keeps threads the user has written in open by default.

## What gets archived

Within the **current environment**, archiving uses the existing placement and delegated family and follows `getThreadOwner` transitively across projects. **Historical threads** whose creator record has no ownership keep their existing archive behavior. **Threads released from ownership** stay outside the added cascade.

**Unarchive** uses the persisted `archivedWith` cohort. An already-archived intermediate keeps its prior restore cohort for legacy placement and lineage. A descendant reached only through recorded ownership restores with the operation that archived it.

## Human-message rule

A child stays open by default when its stored shell records a message with **user role and user authorship**. The `latestUserAuthoredMessageAt` signal excludes agent relays and idle compaction. Renames and other actions that adopt the workspace do not set this message signal.

The child and its downstream family stay owned. If the child is already archived, its messages still protect the live branch. Web and desktop can include the branch with separate confirmation. Mobile previews the default selection and offers Review so the user can open and archive a conversation directly.

## Preview and protections

- **Preview first.** Web, desktop, mobile, and the tool show the family before the wider selection applies. Clients list titles and projects, what stays open, and active, unread, or protected state. The tool returns the environment ID and project IDs.
- **Server re-check.** The server validates the exact selection again under its locks.
- **Saved workspaces.** Each archive selection shows its own discardable and retained copies. The server binds disposal consent to the confirmed thread IDs. A copy shared with a thread that stays open remains available.
- **Bulk.** An explicitly selected child still gets its own archive when the parent's default keeps it open. When two operations share a thread, the later one refreshes and can proceed only when its confirmed selection still matches after removing threads the batch already archived.
- Existing activity, permission, shutdown, and retry protections apply to the selected family.

## Groups

In an archive that uses the new ownership preview, a marked group is eligible when it has no conversations, runs, recorded Project Actions, user adoption, or protection, and every live member is in the same confirmed archive.

It then archives with the family and can be restored from the archive.

## Scope and follow-ups

- **Current environment only.** Linked environments can already preview or archive a known thread ID with the peer's authorization. They cannot enumerate a remote part of a family by owner, or record an offline destination as pending.
- **Cross-environment follow-up.** Use an owner-qualified inventory and a durable operation record. Review the exact selection per reachable peer, use that peer’s authorization, and record its receipt. Show offline or denied peers as pending or blocked and retry idempotently. Restore the cohorts that actually committed.
- **Temporary projects.** PR #761 landed the project listing service. The orchestrator is scheduling removal at wrap-up separately. Promotion landed in #763 and is included through the base.

## Archive performance

Measured on the same 4,866 real thread projections. The dev migration preserved complete histories for 191 visible, stopped threads. The benchmark added the remaining projection rows from a read-only snapshot and archived only the migrated threads.

| Median wall time | Original main | Original PR | Current main | Guarded indexed PR |
| --- | ---: | ---: | ---: | ---: |
| Candidate query | 0.08 ms | 33.85 ms | 0.09 ms | 0.43 ms |
| Family preview | 12.76 ms | 48.19 ms | 13.47 ms | 14.48 ms |
| Archive one 11-thread family | 176 ms | 425 ms | 183 ms | 178 ms |
| Archive 50 threads, including previews | 654 ms | 11.44 s | 671 ms | 778 ms |

Across five alternating pairs per comparison, the original query added a median 10.70 seconds to the 50-thread batch. The guarded indexed query added 108 ms (range +66 to +139 ms). The traced statements account for a median 76 ms of this difference.

The 11-thread command runs the candidate query three times at dispatch and four times at completion. Preview adds one. The bulk ran 284 statements, including 50 previews.

SQLite's original plan materialized `recorded_owner_candidates` and scanned the thread table twice. The new plan seeks both partial expression indexes from the recursive family and group lookups. The expressions guard malformed JSON so legacy recovery can keep running. The recorded restore-cohort lookup runs only when the root has both cohort fields. Ownership qualification stays in `getThreadOwner`.

## Verification

- **Seeded parity.** On a LastCode database seed, current main and the indexed head selected and archived the same 27 threads across three real historical families. Archive events and exact restore cohorts matched. Each case used its own disposable copy, production dispatch, and the production archive runner, with the background worker disabled. The seed stayed byte-for-byte identical.
- **Tests.** Archive and promotion server tests, the saved-workspace regression, Chromium confirmation tests, compiled web tests, and mobile and shared unit tests passed. Quick CI passed on the current head. Three bulk-archive regressions cover shared members in both orders and a refresh that finds new work. The database recovery, migration, and archive suites verify guarded lookups on legacy data and index selection.
- **Browser.** The isolated browser applied the default cascade across two projects, and the user-authored conversation stayed open.
- **Evals.** The new tool eval passed 0/4 on the baseline and 4/4 after the first tool change. The idle control passed 3/3 on both. The rebased tool text passed all 12 scored samples, including 3/3 for the typed-in branch and 2/2 for the idle control. The provider stream ended early on three samples, which remained unscored.
- **Context growth.** The measured agent tool list grew by 314 bytes (85,690 → 86,004). The budget changes by one line.

<details><summary>Browser evidence</summary>

The existing archive menu on the synthetic fixture.

<img width="840" alt="Archive option in the existing thread menu" src="https://github.com/user-attachments/assets/87994587-c683-4fd7-9b1a-3e2cb0353b39" />

The cross-project preview with the typed-in conversation kept open.

<img width="840" alt="Archive preview with projects, selection, and a keep-open conversation" src="https://github.com/user-attachments/assets/da231c65-2c75-49c1-b7f8-00bb46c4e810" />

Opting in to archive the typed-in child now updates the saved-copy list to match the selection. Both captures use the same Chromium fixture.

| Before | After |
| --- | --- |
| <img width="414" alt="After opt-in, the saved copy still appears under retained copies" src="https://github.com/user-attachments/assets/71bd3d69-c49e-43a1-be6f-24559f768658" /> | <img width="414" alt="After opt-in, the shared saved copy appears in the list to discard" src="https://github.com/user-attachments/assets/3176ef20-d563-4775-bf04-b3adeac23810" /> |

</details>

## Upstream-owned files

| File | Added | Removed |
| --- | ---: | ---: |
| `apps/mobile/src/features/home/useThreadListActions.test.ts` | 4 | 0 |
| `apps/server/src/mcp/toolkits/thread/handlers.ts` | 6 | 0 |
| `apps/server/src/mcp/toolkits/thread/tools.ts` | 4 | 2 |
| `apps/server/src/orchestration-v2/Orchestrator.ts` | 6 | 35 |
| `apps/server/src/orchestration-v2/ProjectionStore.ts` | 3 | 1 |
| `apps/server/src/orchestration-v2/ThreadManagementService.ts` | 2 | 1 |
| `apps/server/src/provider/agentContextBudget.json` | 1 | 1 |
| `packages/client-runtime/src/operations/commands.ts` | 8 | 0 |
| `packages/contracts/src/orchestrationV2.ts` | 4 | 0 |
| Each of `apps/mobile/src/i18n/translations/{de,en,es,fr,ja,ko,pt-BR,ru,sv,zh}.json` | 3 | 0 |
| Each of `apps/web/src/i18n/translations/{de,en,es,fr,ja,ko,pt-BR,ru,sv,zh}.json` | 5 | 0 |

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Local review: 4 rounds with GPT-6.1 Terra (xhigh), approved. The fourth round reviewed the final safety changes after churn-gate. A separate scoped source review approved the archive query and index migration.

Carry-Source-Ref: refs/lastcode/carry-sources/pr-764/51687d0e8003745ca2058df231e4d0fcf7e87310
Carry-Source-Base: afdac03
Carry-Source-Head: 51687d0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-context-increase Intentional agent context growth approved by a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant