newSubSession builds a *session.Session from a SubSessionConfig and a parent session. It consolidates the session options that were previously duplicated across handleTaskTransfer and RunAgent.
(parent *session.Session, cfg SubSessionConfig, childAgent *agent.Agent)
| 151 | // session. It consolidates the session options that were previously duplicated |
| 152 | // across handleTaskTransfer and RunAgent. |
| 153 | func newSubSession(parent *session.Session, cfg SubSessionConfig, childAgent *agent.Agent) *session.Session { |
| 154 | // Sub-agents start in a fresh session, so they don't see the user's |
| 155 | // original messages or attached files. Snapshot the parent's attached |
| 156 | // files once and propagate them both to the system prompt (so the agent |
| 157 | // is told about them) and to the child session (so further nested |
| 158 | // transfers keep inheriting them). |
| 159 | attachedFiles := parent.AttachedFilesSnapshot() |
| 160 | |
| 161 | sysMsg := cfg.SystemMessage |
| 162 | if sysMsg == "" { |
| 163 | sysMsg = buildTaskSystemMessage(cfg.Task, cfg.ExpectedOutput, attachedFiles) |
| 164 | } |
| 165 | |
| 166 | userMsg := cfg.ImplicitUserMessage |
| 167 | if userMsg == "" { |
| 168 | userMsg = "Please proceed." |
| 169 | } |
| 170 | |
| 171 | opts := []session.Opt{ |
| 172 | session.WithSystemMessage(sysMsg), |
| 173 | session.WithImplicitUserMessage(userMsg), |
| 174 | session.WithMaxIterations(childAgent.MaxIterations()), |
| 175 | session.WithMaxConsecutiveToolCalls(childAgent.MaxConsecutiveToolCalls()), |
| 176 | session.WithMaxOldToolCallTokens(childAgent.MaxOldToolCallTokens()), |
| 177 | session.WithTitle(cfg.Title), |
| 178 | session.WithToolsApproved(cfg.ToolsApproved), |
| 179 | session.WithNonInteractive(cfg.NonInteractive), |
| 180 | session.WithSendUserMessage(false), |
| 181 | session.WithParentID(parent.ID), |
| 182 | session.WithAttachedFiles(attachedFiles), |
| 183 | } |
| 184 | if cfg.PinAgent { |
| 185 | opts = append(opts, session.WithAgentName(cfg.AgentName)) |
| 186 | } |
| 187 | // Merge parent's excluded tools with config's excluded tools so that |
| 188 | // nested sub-sessions (e.g. skill → transfer_task → child) inherit |
| 189 | // exclusions from all ancestors and don't re-introduce filtered tools. |
| 190 | excludedTools := mergeExcludedTools(parent.ExcludedTools, cfg.ExcludedTools) |
| 191 | if len(excludedTools) > 0 { |
| 192 | opts = append(opts, session.WithExcludedTools(excludedTools)) |
| 193 | } |
| 194 | if len(cfg.AllowedTools) > 0 { |
| 195 | opts = append(opts, session.WithAllowedTools(cfg.AllowedTools)) |
| 196 | } |
| 197 | if len(cfg.ExtraToolSets) > 0 { |
| 198 | opts = append(opts, session.WithExtraToolSets(cfg.ExtraToolSets)) |
| 199 | } |
| 200 | return session.New(opts...) |
| 201 | } |
| 202 | |
| 203 | // mergeExcludedTools combines two excluded-tool lists, deduplicating entries. |
| 204 | // It returns nil when both inputs are empty. |